当前位置:网站首页>QT basic day 2 (2) QT basic components: button class, layout class, output class, input class, container and other individual examples
QT basic day 2 (2) QT basic components: button class, layout class, output class, input class, container and other individual examples
2022-07-29 07:21:00 【Fly 】
We said that before qt Basic development process of program
1. State the necessary components
2. Construct components
3. Necessary layout
4. Front and back desk Association
5. beautify
Today we will continue to talk about qt Basic components of .
One . Button class

Push Button : Button
Tool Button : Tool button
Radio Button : Radio button
Check Button : Check box
Command Link Button : Command link button
Dialog Button Box : Dialog box button box
Example :
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QPushButton>
#include <QToolButton>
#include <QRadioButton>
#include <QCheckBox>
#include <QCommandLinkButton>
#include <QLineEdit>
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; // General button
QToolButton *bt_tool; // Tool button
QRadioButton *bt_radio; // Radio button
QRadioButton *bt_radio1;// Radio button
QCheckBox *bt_check; // Check button
QCheckBox *bt_check1; // Check button
QCommandLinkButton *bt_cmd;// Command button
QLineEdit *le;
QCheckBox *ck;
};
#endif // WIDGET_H#include "widget.h"
#include <QVBoxLayout>
#include <QDebug>
Widget::Widget(QWidget *parent)
: QWidget(parent)
{
bt_button = new QPushButton(" General button ");
bt_tool = new QToolButton;
bt_tool->setText(" Tool button ");
bt_radio = new QRadioButton(" Radio button ");
bt_radio1 = new QRadioButton(" Radio button 1");
bt_check = new QCheckBox(" Check box ");
bt_check1 = new QCheckBox(" Check box 1");
bt_cmd = new QCommandLinkButton(" Baidu ");
bt_cmd->setDescription("www.baidu.com");
// application
le = new QLineEdit;
le->setEchoMode(QLineEdit::Password);
ck = new QCheckBox(" Display password ");
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()
{
} 
Two . Layout class

Vertical Layout: Vertical layout
Horizontal Layout: Horizontal layout
Grid Layout: Grid layout
Form Layout: Form layout
3、 ... and . Output class

Label: label
Text Browser: Text browser
Graphics View: Graphic view
Calendar Widget: The calendar
LCD Number: LCD Digital
Progress Bar: Progress bar
Horizontal Line: Level
Vertical Line: Vertical line
OpenGL Widget:OpenGL Tools
QQuickWidget: The embedded QML Tools
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QLabel>
#include <QTextBrowser>
#include <QCalendarWidget>
#include <QDate>
#include <QLCDNumber>
#include <QProgressBar>
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; // Text label
QLabel *lb_pix; // Picture label
QLabel *lb_gif; // Animated tags
QTextBrowser *tbrowser;
QCalendarWidget *caw;
QLCDNumber *lcd;
QProgressBar *pbr;
};
#endif // WIDGET_H#include "widget.h"
#include <QVBoxLayout>
#include <QMovie>
#include <QTimer>
Widget::Widget(QWidget *parent)
: QWidget(parent)
{
/* label */
lb_text = new QLabel(" Label testing ");
lb_text->setAlignment(Qt::AlignCenter);
lb_pix = new QLabel(" I'm the picture ");
lb_pix->setMinimumSize(10, 10);
lb_pix->setScaledContents(true); // Auto zoom display
lb_pix->setPixmap(QPixmap("C:\\Users\\ThinkPad T490\\Desktop\\220501\\1.bmp"));
lb_gif = new QLabel(" I am animation ");
QMovie *m = new QMovie("C:\\Users\\ThinkPad T490\\Desktop\\220501\\1.gif");
lb_gif->setMovie(m);
m->start();
/* Text browser (html)*/
tbrowser = new QTextBrowser;
tbrowser->setText("<!DOCTYPE html>\
<html>\
<head>\
<meta charset=\"utf-8\">\
<title>runoob.com</title>\
</head>\
<body>\
<h1 >AAAAAAAA</h1>\
<p style=\"background-color:rgb(255,0,0)\">BBBBBBBBB</p>\
</body>\
</html>");
/* Calendar window */
caw = new QCalendarWidget;
/* Seven digital tubes */
lcd = new QLCDNumber;
lcd->setMinimumHeight(50);
lcd->display(250); // Show numbers
/* Progress bar */
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()
{
}

Four . Input class

Combo Box: Combo box
/* Combo box 、 A drop-down box */
cmb = new QComboBox; // Construct combo box
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: Font combo box
/* Font drop-down box */
fcmb = new QFontComboBox;
connect(fcmb, &QFontComboBox::currentFontChanged, [&](QFont f){ lb->setFont(f);});
Line Edit: Row edit box
/* Row edit box */
le = new QLineEdit;
le->setPlaceholderText(" user name ");
Text Edit: Text edit box
/* Text edit box */
te = new QTextEdit;
connect(te, SIGNAL(textChanged()), this, SLOT(te_to_lb()));
Plain Text Edit: Plain text edit box
Spin Box: Digital display box ( Rotating box )
/* Spin frame */
sb = new QSpinBox;
sb->setMaximum(20); // Maximum can only be 20
sb->setSingleStep(10); // Set the single step span
connect(sb, SIGNAL(valueChanged(int)), lcd, SLOT(display(int)));
Double Spin Box:Double Digital display box
Time Edit: Time edit
/* Time edit box */
timee = new QTimeEdit;
connect(timee, SIGNAL(timeChanged(QTime)), this, SLOT(show_time(QTime)));
Date Edit: Date editing
Date/Time Edit: date / Time edit
Dial: dial
Horizontal Scroll Bar: Horizontal scroll bar
Vertical Scroll Bar: Vertical scroll bar
/* Scroll bar */
slb = new QScrollBar;
slb->setRange(0, 1000); // Set the adjustment range
slb->setOrientation(Qt::Horizontal); // Display scheme “ level ”
connect(slb, SIGNAL(valueChanged(int)), lcd, SLOT(display(int)));
Horizontal Slider: Horizontal slider
Vertical Slider: Vertical slider
/* Slide the lever */
sd = new QSlider;
sd->setRange(0, 1000); // Set the adjustment range
sd->setOrientation(Qt::Horizontal); // Display scheme “ level ”
connect(sd, SIGNAL(valueChanged(int)), lcd, SLOT(display(int)));
Key Sequence Edit: Key sequence edit box
5、 ... and . Container class

Group Box: Group box
lb[0] = new QLabel(" subject 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"); // Container with border display and annotation
g1->setLayout(vbox1);
Scroll Area: Scroll area
Tool Box: hold-all
Tab Widget: Label component
Stacked Widget: Stack parts
Frame: frame
Widget: The widget
MDI Area:MDI Area
Dock Widget: Dock form part
QAxWidget: encapsulation Flash Of ActiveX Control
6、 ... and . Space interval group

Horizontal Spacer: Horizontal spacing
Vertical Spacer: Vertical spacing
7、 ... and . Project view group

List View: List view
Tree View: Tree view
Table View: Tabular chart
Column View: Column view
Undo View: Undo view
8、 ... and . Project control group

List Widget: List control
Tree Widget: Tree control
Table Widget: Table control
Object-oriented thinking :
Example :
qustion.h
#ifndef QUSTIONWIDGET_H
#define QUSTIONWIDGET_H
#include <QWidget>
#include <QLabel>
#include <QRadioButton>
#include <QVBoxLayout>
class qustionWidget : public QWidget
{
Q_OBJECT
public:
qustionWidget(QWidget *parent = nullptr);
public:
void setquestion(QString str); // Set the topic
void addAnswer(QString str); // Add options
private:
QLabel *lb; // Show title
//QVector<QRadioButton *> bt;
QVBoxLayout *vbox; // Display options
};
#endif // QUSTIONWIDGET_H
widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include "qustionwidget.h"
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = 0);
~Widget();
qustionWidget *q1;
qustionWidget *q2;
qustionWidget *q3;
};
#endif // WIDGET_Hqustiopn.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; // subject “ Control ”
q1->setquestion("xxxxxxxxxxxxxxxxxxxxxxxxxxx");
q1->addAnswer("aaaaaaa");
q1->addAnswer("bbbbbbb");
q1->addAnswer("ccccccc");
q1->addAnswer("dddddddddd");
q2 = new qustionWidget; // subject “ Control ”
q2->setquestion("yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy");
q2->addAnswer("xxxxx");
q2->addAnswer("yyyyyy");
q2->addAnswer("zzzzzzzzzzzzz");
q3 = new qustionWidget; // subject “ Control ”
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()
{
}
effect :

边栏推荐
猜你喜欢

Cvpr2021 | multi view stereo matching based on self supervised learning (cvpr2021)

MySQL----多表查询

CMOS芯片制造全工艺流程

Full process flow of CMOS chip manufacturing

2022-07-28: what is the output of the following go language code? A:AA; B:AB; C:BA; D:BB。 package main import ( “fmt“ ) func main() { f

SQL优化

JS chicken laying eggs and egg laying chickens. Who appeared earlier, object or function? Is function an instance of function?

MVFuseNet:Improving End-to-End Object Detection and Motion Forecasting through Multi-View Fusion of

Win11 system error: code execution cannot continue because ierutil.dll cannot be found. Reinstalling the program may fix this problem

Excel file reading and writing (creation and parsing)
随机推荐
Redis基础篇
最新百亿量化私募名单
【Unity实战100例】Unity万能答题系统之单选多选判断题全部通用
Remote invocation of microservices
Other basic monitoring items of ZABBIX
Student achievement ranking system based on C language design
我的个人网站不让接入微信登录,于是我做了这个
Variables and encryption in ansible
能在SQL 语句中 指定 内存参数吗?
Implementation of book borrowing management system based on C language
[redis] redis development specifications and precautions
1172. The plate stack has a sequence table + stack
spark学习笔记(七)——sparkcore核心编程-RDD序列化/依赖关系/持久化/分区器/累加器/广播变量
我,28岁,测试员,10月无情被辞:想给还在学测试 的人提个醒......
MySQL 有这一篇就够(呕心狂敲37k字,只为博君一点赞!!!)
JS chicken laying eggs and egg laying chickens. Who appeared earlier, object or function? Is function an instance of function?
Error 1045 (28000) access denied for user 'root' @ 'localhost' solution
作业7.28 文件IO与标准IO
Latest 10 billion quantitative private placement list
MySQL 使用客户端以及SELECT 方式查看 BLOB 类型字段内容总结