以下qt控件的学习记录是做毕设应该是需要用到的。
QPushButton控件
QPushButton是实现按钮点击的控件。在mainwindow.h文件中改写代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| #ifndef MAINWINDOW_H #define MAINWINDOW_H
#include <QMainWindow>
#include <QPushButton>
class MainWindow : public QMainWindow { Q_OBJECT public: MainWindow(QWidget *parent = nullptr); ~MainWindow(); private: QPushButton *pushButton1; QPushButton *pushButton2;
private slots:
void pushButton1_Clicked();
void pushButton2_Clicked(); }; #endif
|
之后编写mainwindow.cpp文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
| #include "mainwindow.h"
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) {
this->setGeometry(0, 0, 800, 480);
pushButton1 = new QPushButton("窗口皮肤 1", this); pushButton2 = new QPushButton("窗口皮肤 2", this);
pushButton1->setGeometry(300,200,80,40); pushButton2->setGeometry(400,200,80,40);
connect(pushButton1, SIGNAL(clicked()), this,SLOT(pushButton1_Clicked())); connect(pushButton2, SIGNAL(clicked()), this,SLOT(pushButton2_Clicked())); }
MainWindow::~MainWindow() { }
void MainWindow::pushButton1_Clicked() {
this->setStyleSheet("QMainWindow { background-color: rgba(255, 245,238, 100%); }"); }
void MainWindow::pushButton2_Clicked() {
this->setStyleSheet("QMainWindow { background-color: rgba(238, 122,233, 100%); }"); }
|
QToolButton控件
工具按钮是带图标的,跟一般的按钮不一样。其实跟PushButton差不多。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| #ifndef MAINWINDOW_H #define MAINWINDOW_H
#include <QMainWindow> #include <QToolButton> #include <QToolBar>
class MainWindow : public QMainWindow { Q_OBJECT
public: MainWindow(QWidget *parent = nullptr); ~MainWindow();
private:
QToolButton *toolButton;
QToolBar *toolBar; }; #endif
|
Main window.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
| #include "mainwindow.h" #include <QApplication> #include <QStyle>
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) {
this->setGeometry(0, 0, 800, 480);
toolBar = new QToolBar(this);
toolBar->setGeometry(0, 0, 800, 100);
QStyle *style = QApplication::style();
QIcon icon =style->standardIcon(QStyle::SP_TitleBarContextHelpButton);
toolButton = new QToolButton();
toolButton->setIcon(icon);
toolButton->setText("帮助");
toolButton->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
toolBar->addWidget(toolButton); }
MainWindow::~MainWindow() { }
|
QCheckBox控件
QCheckBox 继承 QAbstractButton。三态选择框,复选按钮(复选框)与 RadioButton 的区别是选择模式,单选按钮提供多选一,复选按钮提供多选多。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| #ifndef MAINWINDOW_H #define MAINWINDOW_H
#include <QMainWindow>
#include <QCheckBox> class MainWindow : public QMainWindow { Q_OBJECT public: MainWindow(QWidget *parent = nullptr); ~MainWindow();
private:
QCheckBox *checkBox; private slots:
void checkBoxStateChanged(int);
}; #endif
|
mainwindow.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
| #include "mainwindow.h"
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) {
this->setGeometry(0, 0, 800, 480); this->setStyleSheet("QMainWindow {background-color: rgba(100, 100,100, 100%);}");
checkBox = new QCheckBox(this);
checkBox->setGeometry(350, 200, 250, 50);
checkBox->setCheckState(Qt::Checked);
checkBox->setText("初始化为 Checked 状态");
checkBox->setTristate();
connect(checkBox, SIGNAL(stateChanged(int)), this,SLOT(checkBoxStateChanged(int))); } MainWindow::~MainWindow() { }
void MainWindow::checkBoxStateChanged(int state) { switch (state) { case Qt::Checked: checkBox->setText("Checked 状态"); break; case Qt::Unchecked: checkBox->setText("Unchecked 状态"); break; case Qt::PartiallyChecked: checkBox->setText("PartiallyChecked 状态"); break; default: break; } }
|
lineEdit控件
QLineEdit 小部件是一个单行文本编辑器。行编辑允许用户使用一组有用的编辑函数输入和编辑一行纯文本。 编写输入密码的窗口。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| #ifndef MAINWINDOW_H #define MAINWINDOW_H
#include <QMainWindow> #include <QLineEdit> #include <QPushButton> #include <QLabel>
class MainWindow : public QMainWindow { Q_OBJECT
public: MainWindow(QWidget *parent = nullptr); ~MainWindow();
private: QLineEdit *lineEdit; QPushButton *pushButton; QLabel *label1; QLabel *label2;
private slots: void pushButtonClicked(); }; #endif
|
mainwindow.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
| #include "mainwindow.h" #include "string.h" #include <QString> MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) { this->setGeometry( 0 , 0, 800 , 480); lineEdit = new QLineEdit(this); lineEdit -> setGeometry( 280, 200, 200, 20); pushButton = new QPushButton(this); pushButton->setGeometry( 280, 280, 100, 21); pushButton->setText("确认"); label1 = new QLabel(this); label1 ->setGeometry( 280, 240, 400, 20); label1->setText("请输入密码:"); label2 = new QLabel(this); label2 ->setGeometry(280, 300, 400, 20);
connect(pushButton, SIGNAL(clicked()), this, SLOT(pushButtonClicked())); }
MainWindow::~MainWindow() { } void MainWindow::pushButtonClicked() { QString str1; QString str2; QString str3; str3 = lineEdit->text(); str1 = "你输入的内容是"; str2 = "123456"; if(QString::compare(str2, str3, Qt::CaseSensitive) == 0) { label2->setText("密码正确"); }else{ label2->setText("密码错误"); } str1 += lineEdit->text(); label1->setText(str1); lineEdit->clear(); }
|

QFontComboBox控件
调整字体的控件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| #ifndef MAINWINDOW_H #define MAINWINDOW_H
#include <QMainWindow> #include <QFontComboBox> #include <QLabel> class MainWindow : public QMainWindow { Q_OBJECT
public: MainWindow(QWidget *parent = nullptr); ~MainWindow(); private: QFontComboBox *fontComboBox; QLabel *label; private slots: void fontComboBoxFontChanged(QFont); }; #endif
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| #include "mainwindow.h" #include <QLabel> MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) { this->setGeometry( 0, 0, 800, 480); fontComboBox = new QFontComboBox(this); label = new QLabel(this);
fontComboBox ->setGeometry(280, 200, 200, 30); label->setGeometry( 280, 250, 300, 50); connect(fontComboBox, SIGNAL(currentFontChanged()), this, SLOT(fontComboBoxFontChanged(QFont))); } MainWindow::~MainWindow() { } void MainWindow::fontComboBoxFontChanged(QFont font) { label->setFont(font); QString str = "设置的字体为:" + fontComboBox-> itemText(fontComboBox->currentIndex()); label->setText(str); }
|
QcomboBox控件
可以使用在单位的选择上。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| #ifndef MAINWINDOW_H #define MAINWINDOW_H
#include <QMainWindow> #include <QComboBox> class MainWindow : public QMainWindow { Q_OBJECT public: MainWindow(QWidget *parent = nullptr); ~MainWindow(); private: QComboBox *comboBox; private slots: void comboBoxIndexChanged(int); }; #endif
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| #include "mainwindow.h" #include <QDebug> MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) { this->setGeometry( 0, 0, 800, 480); comboBox = new QComboBox(this); comboBox ->setGeometry(300, 200, 150, 30); comboBox->addItem("cm(默认)"); comboBox->addItem("mm"); comboBox->addItem("dm"); connect(comboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(comboBoxIndexChanged(int))); } MainWindow::~MainWindow() { } void MainWindow::comboBoxIndexChanged(int index) {
qDebug()<<"您选择的省份是"<< comboBox->itemText(index)<<endl; }
|
