目录
2. 布局类(水平、垂直、网格、两列)(该类的实例具体与其他类同时使用)
4. 输入类(下拉框、字体下拉框、行编辑框、文本编辑框、自旋框、时间编辑框、旋钮、滚动条、滑动杆儿)
5. 容器(分组框、widget、滚动窗口、工具箱、制表容器、栈式窗口)
一、QT程序开发流程
1. 申明必要的部件(头文件)
2. 将部件构造出来(构造函数)
3. 必要的布局(构造函数)
4. 前后台关联(构造函数)
5. 美化
二、QT基础部件分类
1. 按钮类(普通、工具、单选、复选、命令连接)
案列:
#ifndef WIDGET_H#define WIDGET_H#include <QWidget>#include <QPushButton>#include <QToolButton>#include <QRadioButton>#include <QCheckBox>#include <QCommandLinkButton>#include <QDebug>#include <QLineEdit>QT_BEGIN_NAMESPACEnamespace Ui { class Widget; }QT_END_NAMESPACEclass Widget : public QWidget{Q_OBJECTpublic slots:void xxx(bool);void showpass(bool);public:Widget(QWidget *parent = nullptr);~Widget();private:QPushButton *bt_button;QToolButton *bt_tool;QRadioButton *bt_radio;QRadioButton *bt_radio1;QCheckBox *bt_check;QCheckBox *bt_check1;QCommandLinkButton *bt_command;QLineEdit *le;QCheckBox *ck;Ui::Widget *ui;};#endif // WIDGET_H
#include "widget.h"#include "ui_widget.h"#include <QVBoxLayout>Widget::Widget(QWidget *parent): QWidget(parent), ui(new Ui::Widget){bt_button=new QPushButton("普通按钮");bt_tool=new QToolButton;bt_tool->setText("工具按钮");bt_radio=new QRadioButton("单选按钮");bt_radio1=new QRadioButton("单选按钮");bt_check=new QCheckBox("复选按钮");bt_check1=new QCheckBox("复选按钮");bt_command=new QCommandLinkButton("命令连接按钮");bt_command->setDescription("用来测试的");le=new QLineEdit;ck=new QCheckBox("显示密码");QVBoxLayout *vbox=new QVBoxLayout;vbox->addWidget(bt_button);vbox->addWidget(bt_tool);vbox->addWidget(bt_radio);vbox->addWidget(bt_radio1);vbox->addWidget(bt_check);vbox->addWidget(bt_check1);vbox->addWidget(bt_command);vbox->addWidget(le);vbox->addWidget(ck);setLayout(vbox);connect(bt_button,SIGNAL(clicked(bool)),this,SLOT(xxx(bool)));connect(ck,SIGNAL(toggled(bool)),this,SLOT(showpass(bool)));ui->setupUi(this);}Widget::~Widget(){delete ui;}void Widget::xxx(bool x){qDebug()<<x;}void Widget::showpass(bool x){if(x)le->setEchoMode(QLineEdit::Normal);elsele->setEchoMode(QLineEdit::Password);}
2. 布局类(水平、垂直、网格、两列)(该类的实例具体与其他类同时使用)
3. 输出类(标签、文本浏览器、日历、七段数码管、进度条)
实例:
#ifndef WIDGET_H#define WIDGET_H#include <QWidget>#include <QLabel>#include <QTextBrowser>#include <QCalendarWidget>#include <QLCDNumber>#include <QProgressBar>class Widget : public QWidget{Q_OBJECTpublic slots:void showdate(QDate);void update_value();public:Widget(QWidget *parent = nullptr);~Widget();private:QLabel *lb_text;//文本标签QLabel *lb_pix;//图片标签QLabel *lb_gif;//动画标签QTextBrowser *tbrowser;QCalendarWidget *caw;QLCDNumber *lcd;QProgressBar *pbr;};#endif // WIDGET_H
#include "widget.h"#include <QVBoxLayout>#include <QMovie>#include <QTimer>#include <QTextToSpeech>Widget::Widget(QWidget *parent): QWidget(parent){QTextToSpeech *a=new QTextToSpeech;a->say("123");lb_text=new QLabel("标签测试");lb_text->setAlignment(Qt::AlignCenter);lb_pix=new QLabel("我是图片");lb_pix->setMinimumSize(10,10);lb_pix->setScaledContents(true);lb_pix->setPixmap(QPixmap("D:\\Qt\\hqyj\\1.bmp"));lb_gif=new QLabel("我是动画");QMovie *m=new QMovie("D:\\Qt\\hqyj\\1.gif");lb_gif->setMovie(m);m->start();tbrowser=new QTextBrowser;//文本浏览器tbrowser->setText("html");caw=new QCalendarWidget;//日历lcd=new QLCDNumber;//显示数码管lcd->setMinimumHeight(100);lcd->display(250);pbr=new QProgressBar;//进度条QVBoxLayout *vbox=new QVBoxLayout;vbox->addWidget(lb_text);vbox->addWidget(lb_pix);vbox->addWidget(lb_gif);vbox->addWidget(tbrowser);vbox->addWidget(caw);vbox->addWidget(lcd);vbox->addWidget(pbr);setLayout(vbox);connect(caw,SIGNAL(clicked(QDate)),this,SLOT(showdate(QDate)));QTimer *t=new QTimer;connect(t,SIGNAL(timeout()),this,SLOT(update_value()));t->start(100);}Widget::~Widget(){}void Widget::showdate(QDate d){lb_text->setText(d.toString());}void Widget::update_value(){static int data=0;lcd->display(data);pbr->setValue(data);data++;if(data==100)data=0;}
4. 输入类(下拉框、字体下拉框、行编辑框、文本编辑框、自旋框、时间编辑框、旋钮、滚动条、滑动杆儿)
实例:
#ifndef WIDGET_H#define WIDGET_H#include <QWidget>#include <QComboBox>#include <QDebug>#include <QLabel>#include <QFontComboBox>#include <QTextEdit>#include <QLineEdit>#include <QSpinBox>#include <QLCDNumber>#include <QTimeEdit>#include <QTime>#include <QDial>#include <QScrollBar>#include <QSlider>class Widget : public QWidget{Q_OBJECTpublic slots:void show_int(int );void show_str(QString);void show_pic(QString);void show_time(QTime);void te_to_lb();public:Widget(QWidget *parent = 0);~Widget();private:QComboBox *cmb;QFontComboBox *fcmb;QTextEdit *te;QLineEdit *le;QSpinBox *sb;QTimeEdit *timee;QDial *dl;QScrollBar *slb;QSlider *sd;//调试用QLabel *lb;QLCDNumber *lcd;};#endif // WIDGET_H
#include "widget.h"#include <QVBoxLayout>#include <QFont>Widget::Widget(QWidget *parent): QWidget(parent){lb = new QLabel("你好");lcd = new QLCDNumber;lcd->setMinimumHeight(50);lcd->setDigitCount(10); //设置显示数据位数/*组合框、下拉框*/cmb = new QComboBox; //构造组合框cmb->addItem("C:\\Users\\ThinkPad T490\\Desktop\\220501\\1.bmp");cmb->addItem("C:\\Users\\ThinkPad T490\\Desktop\\220501\\pc.bmp");cmb->addItem("C:\\Users\\ThinkPad T490\\Desktop\\220501\\x.jpg");connect(cmb, SIGNAL(activated(int)), this, SLOT(show_int(int)));connect(cmb, SIGNAL(activated(QString)), this, SLOT(show_str(QString)));connect(cmb, SIGNAL(activated(QString)), lb, SLOT(setText(QString)));//connect(cmb, &QComboBox::activated, [&](int i) { lb->setPixmap(QPixmap(cmb->itemText(i)));});connect(cmb, SIGNAL(activated(QString)), this, SLOT(show_pic(QString)));/*字体下拉框*/fcmb = new QFontComboBox;connect(fcmb, &QFontComboBox::currentFontChanged, [&](QFont f){ lb->setFont(f);});/*行编辑框*/le = new QLineEdit;le->setPlaceholderText("用户名");/*文本编辑框*/te = new QTextEdit;connect(te, SIGNAL(textChanged()), this, SLOT(te_to_lb()));/*自旋框*/sb = new QSpinBox;sb->setMaximum(20); //最大只能为20sb->setSingleStep(10); //设置单步跨度connect(sb, SIGNAL(valueChanged(int)), lcd, SLOT(display(int)));/*时间编辑框*/timee = new QTimeEdit;connect(timee, SIGNAL(timeChanged(QTime)), this, SLOT(show_time(QTime)));/*旋钮*/dl = new QDial;dl->setRange(0, 1000); //设置调节范围connect(dl, SIGNAL(valueChanged(int)), lcd, SLOT(display(int)));/*滚动条*/slb = new QScrollBar;slb->setRange(0, 1000); //设置调节范围slb->setOrientation(Qt::Horizontal); //显示方案“水平”connect(slb, SIGNAL(valueChanged(int)), lcd, SLOT(display(int)));/*滑动杆儿*/sd = new QSlider;sd->setRange(0, 1000); //设置调节范围sd->setOrientation(Qt::Horizontal); //显示方案“水平”connect(sd, SIGNAL(valueChanged(int)), lcd, SLOT(display(int)));QVBoxLayout *vbox = new QVBoxLayout;vbox->addWidget(lb);vbox->addWidget(lcd);vbox->addWidget(le);vbox->addWidget(cmb);vbox->addWidget(fcmb);vbox->addWidget(te);vbox->addWidget(sb);vbox->addWidget(timee);vbox->addWidget(dl);vbox->addWidget(slb);vbox->addWidget(sd);setLayout(vbox);}void Widget::show_time(QTime t){lcd->display(t.toString());}void Widget::te_to_lb(){lb->setText(te->toPlainText());}void Widget::show_pic(QString path){lb->setPixmap(QPixmap(path));}void Widget::show_int(int data){qDebug() << data;}void Widget::show_str(QString str){qDebug() << str;}Widget::~Widget(){}
5. 容器(分组框、widget、滚动窗口、工具箱、制表容器、栈式窗口)
实例(Group Box):
#ifndef WIDGET_H#define WIDGET_H#include <QWidget>#include <QGroupBox>#include <QLabel>#include <QRadioButton>class Widget : public QWidget{Q_OBJECTpublic:Widget(QWidget *parent = 0);~Widget();private:QLabel *lb[2];QRadioButton *rb[6];QGroupBox *g1;QGroupBox *g2;};#endif // WIDGET_H
#include "widget.h"#include <QVBoxLayout>Widget::Widget(QWidget *parent): QWidget(parent){lb[0] = new QLabel("题目1 safasfasfasdf");rb[0] = new QRadioButton("aaaaa");rb[1] = new QRadioButton("bbbbb");rb[2] = new QRadioButton("ccccc");QVBoxLayout *vbox1 = new QVBoxLayout;vbox1->addWidget(lb[0]);vbox1->addWidget(rb[0]);vbox1->addWidget(rb[1]);vbox1->addWidget(rb[2]);g1 = new QGroupBox("AAAA"); //带边框显示和注释的容器g1->setLayout(vbox1);///lb[1] = new QLabel("题目2 dhjdfdjdjfdjdfdjjjjfd");rb[3] = new QRadioButton("xxxxx");rb[4] = new QRadioButton("yyyyy");rb[5] = new QRadioButton("zzzzz");QVBoxLayout *vbox2 = new QVBoxLayout;vbox2->addWidget(lb[1]);vbox2->addWidget(rb[3]);vbox2->addWidget(rb[4]);vbox2->addWidget(rb[5]);g2 = new QGroupBox("BBBB"); //带边框显示和注释的容器g2->setLayout(vbox2);QVBoxLayout *vbox3 = new QVBoxLayout;vbox3->addWidget(g1);vbox3->addWidget(g2);setLayout(vbox3);}Widget::~Widget(){}
实例(scrollarea):
#ifndef WIDGET_H#define WIDGET_H#include <QWidget>#include <QLabel>#include <QScrollArea>class Widget : public QWidget{Q_OBJECTpublic:Widget(QWidget *parent = 0);~Widget();QLabel *lb;QScrollArea *ar;};#endif // WIDGET_H
#include "widget.h"#include <QVBoxLayout>Widget::Widget(QWidget *parent): QWidget(parent){lb = new QLabel;lb->setPixmap(QPixmap("C:\\Users\\ThinkPad T490\\Desktop\\220501\\1.jpeg"));//lb->setParent(this);ar = new QScrollArea; //滚动窗口容器ar->setWidget(lb); //把标签放入容器//ar->setParent(this);QVBoxLayout *vbox = new QVBoxLayout;vbox->addWidget(ar);setLayout(vbox);}Widget::~Widget(){}
实例(stackwidget):
#ifndef WIDGET_H#define WIDGET_H#include <QWidget>#include <QTextEdit>#include <QStackedWidget>#include <QComboBox>class Widget : public QWidget{Q_OBJECTpublic:Widget(QWidget *parent = 0);~Widget();QTextEdit *t1, *t2;QStackedWidget *box;QComboBox *cb;};#endif // WIDGET_H
#include "widget.h"#include <QVBoxLayout>Widget::Widget(QWidget *parent): QWidget(parent){t1 = new QTextEdit("aaaaaaaaaaaaa");t2 = new QTextEdit("bbbbbbbbbbbbbbb");box = new QStackedWidget; //构造一个栈容器box->addWidget(t1); //插入一个新标签box->addWidget(t2); //插入一个新标签//测试cb = new QComboBox;cb->addItem("文本1");cb->addItem("文本2");connect(cb, SIGNAL(activated(int)), box, SLOT(setCurrentIndex(int)));QVBoxLayout *vbox = new QVBoxLayout;vbox->addWidget(cb);vbox->addWidget(box);setLayout(vbox);}Widget::~Widget(){}
实例(tabwidget):
#ifndef WIDGET_H#define WIDGET_H#include <QWidget>#include <QTextEdit>#include <QTabWidget>class Widget : public QWidget{Q_OBJECTpublic slots:void close_tab(int i){box->removeTab(i);}public:Widget(QWidget *parent = 0);~Widget();QTextEdit *t1, *t2;QTabWidget *box;};#endif // WIDGET_H
#include "widget.h"#include <QVBoxLayout>Widget::Widget(QWidget *parent): QWidget(parent){t1 = new QTextEdit("aaaaaaaaaaaaa");t2 = new QTextEdit("bbbbbbbbbbbbbbb");box = new QTabWidget; //构造一个制表容器box->setTabsClosable(true);//显示关闭的按钮box->addTab(t1, "aaaa"); //插入一个新标签box->addTab(t2, "bbbb"); //插入一个新标签QVBoxLayout *vbox = new QVBoxLayout;vbox->addWidget(box);setLayout(vbox);connect(box, SIGNAL(tabCloseRequested(int)), this, SLOT(close_tab(int)));}Widget::~Widget(){}
实例(toolbox):
#ifndef WIDGET_H#define WIDGET_H#include <QWidget>#include <QTextEdit>#include <QToolBox>class Widget : public QWidget{Q_OBJECTpublic:Widget(QWidget *parent = 0);~Widget();QTextEdit *t1, *t2;QToolBox *box;};#endif // WIDGET_H
#include "widget.h"#include <QVBoxLayout>Widget::Widget(QWidget *parent): QWidget(parent){t1 = new QTextEdit("aaaaaaaaaaaaa");t2 = new QTextEdit("bbbbbbbbbbbbbbb");// QVBoxLayout *vbox = new QVBoxLayout;// vbox->addWidget(t1);// vbox->addWidget(t2);// setLayout(vbox);box = new QToolBox; //构造一个工具箱容器box->addItem(t1, "aaaaa"); //将文本编辑框装入工具箱box->addItem(t2, "bbbbb"); //将文本编辑框装入工具箱QVBoxLayout *vbox = new QVBoxLayout;vbox->addWidget(box);setLayout(vbox);}Widget::~Widget(){}
实例(widget):
#ifndef WIDGET_H#define WIDGET_H#include <QWidget>#include <QGroupBox>#include <QLabel>#include <QRadioButton>class Widget : public QWidget{Q_OBJECTpublic:Widget(QWidget *parent = 0);~Widget();private:QLabel *lb[2];QRadioButton *rb[6];QWidget *g1;QWidget *g2;};#endif // WIDGET_H
#include "widget.h"#include <QVBoxLayout>Widget::Widget(QWidget *parent): QWidget(parent){lb[0] = new QLabel("题目1 safasfasfasdf");rb[0] = new QRadioButton("aaaaa");rb[1] = new QRadioButton("bbbbb");rb[2] = new QRadioButton("ccccc");QVBoxLayout *vbox1 = new QVBoxLayout;vbox1->addWidget(lb[0]);vbox1->addWidget(rb[0]);vbox1->addWidget(rb[1]);vbox1->addWidget(rb[2]);g1 = new QWidget; //无边框显示的容器g1->setLayout(vbox1);///lb[1] = new QLabel("题目2 dhjdfdjdjfdjdfdjjjjfd");rb[3] = new QRadioButton("xxxxx");rb[4] = new QRadioButton("yyyyy");rb[5] = new QRadioButton("zzzzz");QVBoxLayout *vbox2 = new QVBoxLayout;vbox2->addWidget(lb[1]);vbox2->addWidget(rb[3]);vbox2->addWidget(rb[4]);vbox2->addWidget(rb[5]);g2 = new QWidget; //无边框显示的容器g2->setLayout(vbox2);QVBoxLayout *vbox3 = new QVBoxLayout;vbox3->addWidget(g1);vbox3->addWidget(g2);setLayout(vbox3);}Widget::~Widget(){
面对对象实例:
questionwidget.h
#ifndef QUSTIONWIDGET_H#define QUSTIONWIDGET_H#include <QWidget>#include <QLabel>#include <QRadioButton>#include <QVBoxLayout>class qustionWidget : public QWidget{Q_OBJECTpublic:qustionWidget(QWidget *parent = nullptr);public:void setquestion(QString str); //设置题目void addAnswer(QString str); //添加选项private:QLabel *lb; //显示题目//QVector<QRadioButton *> bt;QVBoxLayout *vbox; //显示选项的};#endif // QUSTIONWIDGET_H
widget.h
#ifndef WIDGET_H#define WIDGET_H#include <QWidget>#include "qustionwidget.h"class Widget : public QWidget{Q_OBJECTpublic:Widget(QWidget *parent = 0);~Widget();qustionWidget *q1;qustionWidget *q2;qustionWidget *q3;};#endif // WIDGET_H
qustionwidget.cpp
#include "qustionwidget.h"qustionWidget::qustionWidget(QWidget *parent) :QWidget(parent){lb = new QLabel;//bt.clear();vbox = new QVBoxLayout;vbox->addWidget(lb);setLayout(vbox);}void qustionWidget::setquestion(QString str){lb->setText(str);}void qustionWidget::addAnswer(QString str){QRadioButton *an = new QRadioButton(str);vbox->addWidget(an);}
widget.cpp
#include "widget.h"#include <QVBoxLayout>Widget::Widget(QWidget *parent): QWidget(parent){q1 = new qustionWidget; //题目“控件”q1->setquestion("xxxxxxxxxxxxxxxxxxxxxxxxxxx");q1->addAnswer("aaaaaaa");q1->addAnswer("bbbbbbb");q1->addAnswer("ccccccc");q1->addAnswer("dddddddddd");q2 = new qustionWidget; //题目“控件”q2->setquestion("yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy");q2->addAnswer("xxxxx");q2->addAnswer("yyyyyy");q2->addAnswer("zzzzzzzzzzzzz");q3 = new qustionWidget; //题目“控件”q3->setquestion("yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy");q3->addAnswer("xxxxx");q3->addAnswer("yyyyyy");q3->addAnswer("zzzzzzzzzzzzz");QVBoxLayout *vbox = new QVBoxLayout;vbox->addWidget(q1);vbox->addWidget(q2);vbox->addWidget(q3);setLayout(vbox);}Widget::~Widget(){}