当前位置:网站首页>QT topic: basic components (button class, layout class, output class, input class, container class)
QT topic: basic components (button class, layout class, output class, input class, container class)
2022-07-29 07:24:00 【Astray King】
Catalog
Today, let's meet QT Some basic components in , This is the basis of our graphical interface programming , Need to remember !
Button class
1. General button QPushButton
The header file
#include <QPushButton>
To use a button, you need to declare it in the header file
QPushButton *bt_button; // Declare a normal button
Then put the button part new come out :
bt_button=new QPushButton;
2. Tool button QToolButton
The header file
#include <QToolButton>
To use a button, you need to declare it in the header file
QToolButton *bt_tool; // Declare a tool button
Then put the button part new come out :
bt_tool=new QToolButton;
3. Radio button QRadioButton
The header file
#include <QRadioButton>
To use a button, you need to declare it in the header file
QRadioButton *bt_radio; // Declare a radio button
Then put the button part new come out :
bt_radio=new QRadioButton;
4. Check button QCheckBox
The header file
#include <QCheckBox>
To use a button, you need to declare it in the header file
QCheckBox *bt_check; // Declare a check button
Then put the button part new come out :
bt_check=new QCheckBox;
5. Command button QCommandLinkButton
The header file
#include <QCommandLinkButton>
To use a button, you need to declare it in the header file
QCommandLinkButton *bt_cmd; // Declare a command button
Then put the button part new come out :
bt_cmd=new QCommandLinkButton;
We can perform some operations through command buttons, such as jumping to a website
Now let's show a piece of code , We will show the above buttons
First declare the button in the header file :
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;// Row edit box
QCheckBox *ck;Later on cpp File :
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;// Vertical layout
vbox->addWidget(bt_button);// Add buttons to the vertical layout
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);// Paste the layout on the main interface
connect(bt_check, SIGNAL(toggled(bool)), this, SLOT(xxx(bool)));// Front and rear connections
connect(ck, SIGNAL(toggled(bool)), this, SLOT(showpass(bool)));
}
void Widget::showpass(bool x)
{
if(!x)
le->setEchoMode(QLineEdit::Password);// Display in encrypted form
else
le->setEchoMode(QLineEdit::Normal);// Display in the normal way
}
void Widget::xxx(bool x)
{
qDebug() << x;
}Let's take a look at the interface after running :

Of course, we can connect through the front and rear platforms , Use the slot function to give different functions to the keys !!connect() function
connect() The four arguments in the function are : A signal sent by an object 、 Transmitted signal 、 The object receiving the signal and the slot to be executed , Signals and slots should be used separately SIGNAL() and SLOT() The macro is enclosed .
Layout class
1. Horizontal layout QHBoxLayout
2. Vertical layout QVBoxLayout
3. Grid layout QGridLayout
Output class
1. label QLabel
The header file
#include <QLabel>
It needs to be declared before use , Our label components can display different types of content , Can display text , You can also display pictures and animations !
QLabel *lb_text;// Show text tags
QLabel *lb_pix;// display picture
QLabel *lb_gif;// Show animation
Of course, we need to use different functions to realize the corresponding functions
1.setAlignment(QT::AlignCenter): centered
2.setMinimumSize(x,y); Set the minimum zoom size of the picture
3.setScaledContents(true); Auto zoom display , In this way, the part will scale with the interface
4.setPixmap(QPixmap(" Picture path ")); display picture
5.setMovie(m);// Show animation
QMovie *m=new QMovie( route )
m->start();// Start
2. Text browser QTextBrowser
/* 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>");
The text browser can display html Formatted text
3. The calendar QCalendarWidget
/* Calendar window */
caw = new QCalendarWidget;
connect(caw, SIGNAL(clicked(QDate)), this, SLOT(showDate(QDate)));// The date of clicking can be displayed in other parts , Like text tags 4. Seven digital tubes QLcdNumber
/* Seven digital tubes */
lcd = new QLCDNumber;
lcd->setMinimumHeight(50);
lcd->display(250); // Show numbers 5. Progress bar QProgressBar
/* Progress bar */
pbr = new QProgressBar;
pbr->setValue(60);Now let's use these components to form a graphical interface , The code is as follows :
Widget::Widget(QWidget *parent)
: QWidget(parent)
{
/* label */
lb_text = new QLabel(" Label testing ");
lb_text->setAlignment(Qt::AlignCenter);// centered
lb_pix = new QLabel(" I'm the picture ");
lb_pix->setMinimumSize(300, 300);
lb_pix->setScaledContents(true); // Auto zoom display
lb_pix->setPixmap(QPixmap("C:\\Users\\admin\\Desktop\\ picture \\pic4"));
lb_gif = new QLabel(" I am animation ");
QMovie *m = new QMovie("C:\\Users\\admin\\Desktop\\ picture \\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);
// Add vertical layout
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()));// Timer
t->start(100);
}
void Widget::update_value(void)
{
static int data = 0;
lcd->display(data);// Set the nixie tube to display numbers
pbr->setValue(data);// Set the progress bar to display numbers
data++;
if(data == 100)
data = 0;
}
void Widget::showDate(QDate d)
{
lb_text->setText(d.toString());// The date we clicked is displayed in the text tab
}
Let's see the results :

Input class
1. Combo box , A drop-down box QComboBox
/* Combo box 、 A drop-down box */
cmb = new QComboBox; // Construct combo box
cmb->addItem("C:\\Users\\admin\\Desktop\\ picture \\pic4");
cmb->addItem("C:\\Users\\admin\\Desktop\\ picture \\pic3");
cmb->addItem("C:\\Users\\admin\\Desktop\\ picture \\pic2");// Add options 2. Font drop-down box QFontComboBox
/* Font drop-down box */
fcmb = new QFontComboBox;e
connect(fcmb, &QFontComboBox::currentFontChanged, [&](QFont f){ lb->setFont(f);});
3. Row edit box QLineEdit
/* Row edit box */
le = new QLineEdit;
le->setPlaceholderText(" user name "); 4. Text edit box QTextEdit
/* Text edit box */
te = new QTextEdit;
connect(te, SIGNAL(textChanged()), this, SLOT(te_to_lb()));
5. Spin button QSpinBox
/* Spin frame */
sb = new QSpinBox;
sb->setMaximum(200); // Maximum can only be 20
sb->setSingleStep(5); // Set the single step span
connect(sb, SIGNAL(valueChanged(int)), lcd, SLOT(display(int)));
6. Time editor QTimeEdit
/* Time edit box */
timee = new QTimeEdit;
connect(timee, SIGNAL(timeChanged(QTime)), this, SLOT(show_time(QTime)));7. knob QDial
/* knob */
dl = new QDial;
dl->setRange(0, 1000); // Set the adjustment range
connect(dl, SIGNAL(valueChanged(int)), lcd, SLOT(display(int)));
8. Scroll bar QScrollBar
/* 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)));
9. Slide bar QSlider
/* 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)));
Combine the above components :
#include "widget.h"
#include <QVBoxLayout>
#include <QFont>
Widget::Widget(QWidget *parent)
: QWidget(parent)
{
lb = new QLabel(" Hello ");
lcd = new QLCDNumber;
lcd->setMinimumHeight(50);
lcd->setDigitCount(10); // Set the number of display data
/* Combo box 、 A drop-down box */
cmb = new QComboBox; // Construct combo box
cmb->addItem("C:\\Users\\admin\\Desktop\\ picture \\pic4");
cmb->addItem("C:\\Users\\admin\\Desktop\\ picture \\pic3");
cmb->addItem("C:\\Users\\admin\\Desktop\\ picture \\pic2");
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 drop-down box */
fcmb = new QFontComboBox;
connect(fcmb, &QFontComboBox::currentFontChanged, [&](QFont f){ lb->setFont(f);});
/* Row edit box */
le = new QLineEdit;
le->setPlaceholderText(" user name ");
/* Text edit box */
te = new QTextEdit;
connect(te, SIGNAL(textChanged()), this, SLOT(te_to_lb()));
/* Spin frame */
sb = new QSpinBox;
sb->setMaximum(200); // Maximum can only be 20
sb->setSingleStep(5); // Set the single step span
connect(sb, SIGNAL(valueChanged(int)), lcd, SLOT(display(int)));
/* Time edit box */
timee = new QTimeEdit;
connect(timee, SIGNAL(timeChanged(QTime)), this, SLOT(show_time(QTime)));
/* knob */
dl = new QDial;
dl->setRange(0, 1000); // Set the adjustment range
connect(dl, SIGNAL(valueChanged(int)), lcd, SLOT(display(int)));
/* 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)));
/* 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)));
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());// Display time through nixie tube
}
void Widget::te_to_lb()
{
lb->setText(te->toPlainText());// Display the contents of the text box to the text label
}
void Widget::show_pic(QString path)
{
lb->setPixmap(QPixmap(path));// display picture
}
void Widget::show_int(int data)
{
qDebug() << data;
}
void Widget::show_str(QString str)
{
qDebug() << str;
}
See how it works :

Container class
1. Group box QGroupBox
#include "widget.h"
#include <QVBoxLayout>
Widget::Widget(QWidget *parent)
: QWidget(parent)
{
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);
///
lb[1] = new QLabel(" subject 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"); // Container with border display and annotation
g2->setLayout(vbox2);
QVBoxLayout *vbox3 = new QVBoxLayout;
vbox3->addWidget(g1);
vbox3->addWidget(g2);
setLayout(vbox3);
}Running results :

2.widget
#include "widget.h"
#include <QVBoxLayout>
Widget::Widget(QWidget *parent)
: QWidget(parent)
{
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 QWidget; // Borderless container
g1->setLayout(vbox1);
///
lb[1] = new QLabel(" subject 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; // Borderless container
g2->setLayout(vbox2);
QVBoxLayout *vbox3 = new QVBoxLayout;
vbox3->addWidget(g1);
vbox3->addWidget(g2);
setLayout(vbox3);
}Running results :

3. Scroll the window QScrollArea
#include "widget.h"
#include <QVBoxLayout>
Widget::Widget(QWidget *parent)
: QWidget(parent)
{
lb = new QLabel;
lb->setPixmap(QPixmap("C:\\Users\\admin\\Desktop\\ picture \\pic2"));
//lb->setParent(this);
ar = new QScrollArea; // Scroll window container
ar->setWidget(lb); // Put the label into the container
//ar->setParent(this);
QVBoxLayout *vbox = new QVBoxLayout;
vbox->addWidget(ar);
setLayout(vbox);
}Running results :

4. hold-all QToolBox
#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; // Construct a toolbox container
box->addItem(t1, "aaaaa"); // Load the text edit box into the toolbox
box->addItem(t2, "bbbbb"); // Load the text edit box into the toolbox
QVBoxLayout *vbox = new QVBoxLayout;
vbox->addWidget(box);
setLayout(vbox);
}Running results :


5. Tabulation container QTabWidget
#include "widget.h"
#include <QVBoxLayout>
Widget::Widget(QWidget *parent)
: QWidget(parent)
{
t1 = new QTextEdit("aaaaaaaaaaaaa");
t2 = new QTextEdit("bbbbbbbbbbbbbbb");
box = new QTabWidget; // Construct a tabulation container
box->setTabsClosable(true);// Show the close button
box->addTab(t1, "aaaa"); // Insert a new label
box->addTab(t2, "bbbb"); // Insert a new label
QVBoxLayout *vbox = new QVBoxLayout;
vbox->addWidget(box);
setLayout(vbox);
connect(box, SIGNAL(tabCloseRequested(int)), this, SLOT(close_tab(int)));
}
Running results :

6. Trestle window QStackedWidget
#include "widget.h"
#include <QVBoxLayout>
Widget::Widget(QWidget *parent)
: QWidget(parent)
{
t1 = new QTextEdit("aaaaaaaaaaaaa");
t2 = new QTextEdit("bbbbbbbbbbbbbbb");
box = new QStackedWidget; // Construct a stack container
box->addWidget(t1); // Insert a new label
box->addWidget(t2); // Insert a new label
// test
cb = new QComboBox;
cb->addItem(" Text 1");
cb->addItem(" Text 2");
connect(cb, SIGNAL(activated(int)), box, SLOT(setCurrentIndex(int)));
QVBoxLayout *vbox = new QVBoxLayout;
vbox->addWidget(cb);
vbox->addWidget(box);
setLayout(vbox);
}Running results :


边栏推荐
- JS break and continue and return keywords
- [redis] redis development specifications and precautions
- 暑期总结(二)
- Kubernetes (V) -- deploy kubernetes dashboard
- Leetcode 209. subarray with the smallest length (2022.07.28)
- 20-40k | mecarmand 3D vision algorithm / software / Product Manager Recruitment
- Gin service exit
- NPM install reports an error NPM err could not resolve dependency NPM err peer
- 【Unity实战100例】Unity万能答题系统之单选多选判断题全部通用
- Some learning and understanding of vintage analysis
猜你喜欢

Redis基础篇

MySQL advanced (Advanced) SQL statement (I)

Spark Learning Notes (VII) -- spark core core programming - RDD serialization / dependency / persistence / partition / accumulator / broadcast variables

Problems encountered in vmware16 installing virtual machines

如何与斯堪尼亚SCANIA建立EDI连接?

After three years of outsourcing, the salary of automatic testing after job hopping is twice that of the original. The secret is

WPF simple login page completion case

CMOS芯片制造全工艺流程

It's enough for MySQL to have this article (disgusting and crazy typing 37k words, just for Bo Jun's praise!!!)

Variables and encryption in ansible
随机推荐
自定义事件
Thoroughly understand kubernetes scheduling framework and plug-ins
MySQL 使用客户端以及SELECT 方式查看 BLOB 类型字段内容总结
作业7.28 文件IO与标准IO
Gin parameter validation
MySQL - multi table query
Ansible中的变量及加密
Vmware16 create virtual machine: cannot create a new virtual machine, do not have permission to perform this operation
以太网接口介绍
js中break与continue和return关键字
1-后台项目搭建
反射reflect
3-global exception handling
npm install报错npm ERR Could not resolve dependency npm ERR peer
[solution] error: lib/bridge_ generated. dart:837:9: Error: The parameter ‘ptr‘ of the method ‘FlutterRustB
route的meta配置项
Gin service exit
我,28岁,测试员,10月无情被辞:想给还在学测试 的人提个醒......
Synchronous / asynchronous, blocking / non blocking and IO
[redis] redis development specifications and precautions