前面我们说了qt程序的基本开发流程
1. 申明必要的部件
2. 将部件构造出来
3. 必要的布局
4. 前后台关联
5. 美化
今天我们接着来说一说qt的基础部件。
一.按钮类
Push Button
:按钮
Tool Button
:工具按钮
Radio Button
:单选按钮
Check Button
:复选框
Command Link Button
:命令链接按钮
Dialog Button Box
:对话框按钮盒
例子:
#ifndef WIDGET_H
#define WIDGET_H
#include
#include
#include
#include
#include
#include
#include
class Widget : public QWidget
{
Q_OBJECT
public slots:
void xxx(bool x);
void showpass(bool);
public:
Widget(QWidget *parent = 0);
~Widget();
private:
QPushButton *bt_button; //普通按钮
QToolButton *bt_tool; //工具按钮
QRadioButton *bt_radio; //单选按钮
QRadioButton *bt_radio1;//单选按钮
QCheckBox *bt_check; //复选按钮
QCheckBox *bt_check1; //复选按钮
QCommandLinkButton *bt_cmd;//命令按钮
QLineEdit *le;
QCheckBox *ck;
};
#endif // WIDGET_H
#include "widget.h"
#include
#include
Widget::Widget(QWidget *parent)
: QWidget(parent)
{
bt_button = new QPushButton("普通按钮");
bt_tool = new QToolButton;
bt_tool->setText("工具按钮");
bt_radio = new QRadioButton("单选按钮");
bt_radio1 = new QRadioButton("单选按钮1");
bt_check = new QCheckBox("复选框");
bt_check1 = new QCheckBox("复选框1");
bt_cmd = new QCommandLinkButton("百度");
bt_cmd->setDescription("www.baidu.com");
//应用
le = new QLineEdit;
le->setEchoMode(QLineEdit::Password);
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_cmd);
vbox->addWidget(le);
vbox->addWidget(ck);
setLayout(vbox);
// connect(bt_button, SIGNAL(clicked(bool)), this, SLOT(xxx(bool)));
// connect(bt_radio, SIGNAL(toggled(bool)), this, SLOT(xxx(bool)));
connect(bt_check, SIGNAL(toggled(bool)), this, SLOT(xxx(bool)));
connect(ck, SIGNAL(toggled(bool)), this, SLOT(showpass(bool)));
}
void Widget::showpass(bool x)
{
if(!x)
le->setEchoMode(QLineEdit::Password);
else
le->setEchoMode(QLineEdit::Normal);
}
void Widget::xxx(bool x)
{
qDebug() << x;
}
Widget::~Widget()
{
}
二.布局类
Vertical Layout
:垂直布局
Horizontal Layout
:水平布局
Grid Layout
:网格布局
Form Layout
:表单布局
三.输出类
Label:标签
Text Browser:文本浏览器
Graphics View:图形视图
Calendar Widget:日历
LCD Number:液晶数字
Progress Bar:进度条
Horizontal Line:水平线
Vertical Line:垂直线
OpenGL Widget:OpenGL 工具
QQuickWidget:嵌入QML工具
#ifndef WIDGET_H
#define WIDGET_H
#include
#include
#include
#include
#include
#include
#include
class Widget : public QWidget
{
Q_OBJECT
public slots:
void showDate(QDate);
void update_value(void);
public:
Widget(QWidget *parent = 0);
~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
#include
#include
Widget::Widget(QWidget *parent)
: QWidget(parent)
{
/*标签*/
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("C:\\Users\\ThinkPad T490\\Desktop\\220501\\1.bmp"));
lb_gif = new QLabel("我是动画");
QMovie *m = new QMovie("C:\\Users\\ThinkPad T490\\Desktop\\220501\\1.gif");
lb_gif->setMovie(m);
m->start();
/*文本浏览器(html)*/
tbrowser = new QTextBrowser;
tbrowser->setText("\
\
\
\
runoob.com \
\
\
AAAAAAAA
\
BBBBBBBBB
\
\
");
/*日历窗口*/
caw = new QCalendarWidget;
/*七段数码管*/
lcd = new QLCDNumber;
lcd->setMinimumHeight(50);
lcd->display(250); //显示数字
/*进度条*/
pbr = new QProgressBar;
pbr->setValue(60);
QVBoxLayout *vbox = new QVBoxLayout;
vbox->addWidget(pbr);
vbox->addWidget(lcd);
vbox->addWidget(caw);
vbox->addWidget(tbrowser);
vbox->addWidget(lb_text);
vbox->addWidget(lb_pix);
vbox->addWidget(lb_gif);
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);
}
void Widget::update_value(void)
{
static int data = 0;
lcd->display(data);
pbr->setValue(data);
data++;
if(data == 100)
data = 0;
}
void Widget::showDate(QDate d)
{
lb_text->setText(d.toString());
}
Widget::~Widget()
{
}
Combo Box:组合框
/*组合框、下拉框*/
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)));
Font Combo Box:字体组合框
/*字体下拉框*/
fcmb = new QFontComboBox;
connect(fcmb, &QFontComboBox::currentFontChanged, [&](QFont f){ lb->setFont(f);});
Line Edit:行编辑框
/*行编辑框*/
le = new QLineEdit;
le->setPlaceholderText("用户名");
Text Edit:文本编辑框
/*文本编辑框*/
te = new QTextEdit;
connect(te, SIGNAL(textChanged()), this, SLOT(te_to_lb()));
Plain Text Edit:纯文本编辑框
Spin Box:数字显示框(旋转盒)
/*自旋框*/
sb = new QSpinBox;
sb->setMaximum(20); //最大只能为20
sb->setSingleStep(10); //设置单步跨度
connect(sb, SIGNAL(valueChanged(int)), lcd, SLOT(display(int)));
Double Spin Box:Double 数字显示框
Time Edit:时间编辑
/*时间编辑框*/
timee = new QTimeEdit;
connect(timee, SIGNAL(timeChanged(QTime)), this, SLOT(show_time(QTime)));
Date Edit:日期编辑
Date/Time Edit:日期/时间编辑
Dial:拨号
Horizontal Scroll Bar:水平滚动条
Vertical Scroll Bar:垂直滚动条
/*滚动条*/
slb = new QScrollBar;
slb->setRange(0, 1000); //设置调节范围
slb->setOrientation(Qt::Horizontal); //显示方案“水平”
connect(slb, SIGNAL(valueChanged(int)), lcd, SLOT(display(int)));
Horizontal Slider:水平滑块
Vertical Slider:垂直滑块
/*滑动杆儿*/
sd = new QSlider;
sd->setRange(0, 1000); //设置调节范围
sd->setOrientation(Qt::Horizontal); //显示方案“水平”
connect(sd, SIGNAL(valueChanged(int)), lcd, SLOT(display(int)));
Key Sequence Edit:按键序列编辑框
五.容器类
Group Box:组框
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);
Scroll Area:滚动区域
Tool Box:工具箱
Tab Widget:标签部件
Stacked Widget:堆叠部件
Frame:框架
Widget:小部件
MDI Area:MDI区域
Dock Widget:停靠窗体部件
QAxWidget:封装Flash的ActiveX控件
六.空间间隔组
Horizontal Spacer
:水平间隔
Vertical Spacer
:垂直间隔
七.项目视图组
List View
:列表视图
Tree View
:树形视图
Table View
:表格式图
Column View
:列视图
Undo View
:撤消视图
八.项目控件组
List Widget
:列表控件
Tree Widget
:树形控件
Table Widget
:表格控件
面向对象的思想:
例子:
qustion.h
#ifndef QUSTIONWIDGET_H
#define QUSTIONWIDGET_H
#include
#include
#include
#include
class qustionWidget : public QWidget
{
Q_OBJECT
public:
qustionWidget(QWidget *parent = nullptr);
public:
void setquestion(QString str); //设置题目
void addAnswer(QString str); //添加选项
private:
QLabel *lb; //显示题目
//QVector bt;
QVBoxLayout *vbox; //显示选项的
};
#endif // QUSTIONWIDGET_H
widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include
#include "qustionwidget.h"
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = 0);
~Widget();
qustionWidget *q1;
qustionWidget *q2;
qustionWidget *q3;
};
#endif // WIDGET_H
qustiopn.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
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()
{
}
效果: