以下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>
/* 引入 QPushButton 类 */
#include <QPushButton>

class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private:
/* 声明一个 QPushButton 对象 pushButton1 */
QPushButton *pushButton1;
/* 声明一个 QPushButton 对象 pushButton2 */
QPushButton *pushButton2;

private slots:
/* 声明对象 pushButton1 的槽函数 */
void pushButton1_Clicked();
/* 声明对象 pushButton2 的槽函数 */
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)
{
/* 设置宽高为 800×480,位置在 0, 0。(0, 0)代表原点, Qt 默认最左上角的点为原
*/
this->setGeometry(0, 0, 800, 480);

/* 实例化两个按钮对象,并设置其显示文本为窗口皮肤 1 和窗口皮肤 2 */
pushButton1 = new QPushButton("窗口皮肤 1", this);
pushButton2 = new QPushButton("窗口皮肤 2", this);

/* 设定两个 QPushButton 对象的位置 */
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()
{
/* 设置主窗口的样式 1 */
this->setStyleSheet("QMainWindow { background-color: rgba(255, 245,238, 100%); }");
}

/* 槽函数的实现 */
void MainWindow::pushButton2_Clicked()
{
/* 设置主窗口的样式 2 */
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>
/* 引入 QToolButton 类 */
#include <QToolButton>
/* 引入 QToolBar 类 */
#include <QToolBar>

class MainWindow : public QMainWindow
{
Q_OBJECT

public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();

private:
/* 声明一个 QToolButton 对象 */
QToolButton *toolButton;
/* 声明一个 QToolBar 对象 */
QToolBar *toolBar;
};
#endif // MAINWINDOW_H

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);

/* 实例化 QToolBar 对象 */
toolBar = new QToolBar(this);
/* 设置 toolBar 的位置和大小 */
toolBar->setGeometry(0, 0, 800, 100);

/* 实例化 QStyle 类对象,用于设置风格,调用系统类自带的图标 */
QStyle *style = QApplication::style();

/* 使用 Qt 自带的标准图标,可以在帮助文档里搜索 QStyle::StandardPixmap */
QIcon icon =style->standardIcon(QStyle::SP_TitleBarContextHelpButton);

/* 实例化 QToolButton 对象 */
toolButton = new QToolButton();

/* 设置图标 */
toolButton->setIcon(icon);
/* 设置要显示的文本 */
toolButton->setText("帮助");
/* 调用 setToolButtonStyle()方法,设置 toolButoon 的样式,设置为文本置于图标下方 */
toolButton->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);

/* 最后将 toolButton 添加到 ToolBar 里 */
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>
/* 引入 QCheckBox */
#include <QCheckBox>
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();

private:
/* 声明一个 QCheckBox 对象 */
QCheckBox *checkBox;
private slots:
/* 声明 QCheckBox 的槽函数,并带参数传递,用这个参数接收信号的参数 */
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);

/* 设置 QCheckBox 位置和显示大小 */
checkBox->setGeometry(350, 200, 250, 50);
/* 初始化三态复选框的状态为 Checked */
checkBox->setCheckState(Qt::Checked);

/* 设置显示的文本 */
checkBox->setText("初始化为 Checked 状态");
/* 开启三态模式,必须开启,否则只有两种状态,即 Checked 和 Unchecked */
checkBox->setTristate();

/* 连接 checkBox 的信号 stateChanged(int),与我们定义的槽
checkBoxStateChanged(int)连接 */
connect(checkBox, SIGNAL(stateChanged(int)), this,SLOT(checkBoxStateChanged(int)));
}
MainWindow::~MainWindow()
{
}
/* 槽函数的实现 */
void MainWindow::checkBoxStateChanged(int state)
{
/* 判断 checkBox 的 state 状态,设置 checkBox 的文本 */
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_H

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 // MAINWINDOW_H
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 // MAINWINDOW_H
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;
}