当前位置:网站首页>Creation and simple application of QPushButton button button
Creation and simple application of QPushButton button button
2022-07-27 08:27:00 【Bitongo】
List of articles
Preface
stay QT In the program , Every time you add a control , You must declare the header file of the corresponding control at the beginning . If the button is QT One of the most commonly used controls in , Button controls are in English PushButton Definition , but QT There is a feature in the header file declaration of , Is to add a capital letter in front of the header file Q, The first letter of each word is capitalized .
One 、 Button creation steps
- Define header file
#include<QPushButton>
- First you need to create a button
QPushButton * btn1 = new QPushButton;
- Let the buttons you set depend on the window , Here, the window parameter represents the dependent window , That is, the parent class , from c++ You can learn , You can use it here this Pointer instead of .
btn1->setParent(this);
- Name the button
`btn1->setText(" The first button "); // Show text `
- Move button , At first, the default is (0,0) Point position , Upper left corner , You can move it by setting , Move to the position you want .
btn1->move(200,200); // Move btn1 Button
- Set the size of the button
btn2->resize(100,50); // To redraw btn2 size
- Display button
btn1->show();
Two 、 Simple application of buttons
The functions realized here are : Click the set button , Close the window
So you need to connect the window and the button , The English word connected is called connect
The specific use is connect( Parameters 1, Parameters 2, Parameters 3, Parameters 4);
Parameter one : Signal sender
Parameter two : Transmitted signal ( Address of function )
Parameter 3 : The receiver of the signal
Parameter 4 : Processing slot function
You can know ad locum :
Parameter 1 is button , namely btn1;
Parameter 2 is the function of clicking the button , use &QPushButton::clicked Express ;
Parameter 3 is window , Receiving a signal , by this The pointer ;
Parameter 4 is the function of closing the window , Commonly known as slot function , The closed function is &Widget::close.
therefore
// function : Click on “ The first button ” Close the window
connect(btn1,&QPushButton::clicked,this,&Widget::close);
3、 ... and 、 Complete code
widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
class Widget : public QWidget
{
Q_OBJECT // Signal slot
public:
Widget(QWidget *parent = 0);
~Widget();
};
#endif // WIDGET_H
widget.cpp
#include "widget.h"
#include<QPushButton>
Widget::Widget(QWidget *parent) :
QWidget(parent)
{
// Create a button
QPushButton *btn1=new QPushButton;
btn1->show();
btn1->setParent(this); // Give Way btn1 Objects depend on Widget Window
btn1->setText(" The first button "); // Show text
btn1->move(200,200); // Move btn1 Button
// function : Click on “ The first button ” Close the window
// Parameter one : Signal sender Parameter two : Transmitted signal ( Address of function ) Parameter 3 : The receiver of the signal Parameter 4 : Processing slot function
connect(btn1,&QPushButton::clicked,this,&Widget::close);
}
Widget::~Widget()
{
}
main.cpp
#include "widget.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv); //a Application object , stay QT in , The application object has and only has one
Widget w; // Window object ,Widget Parent class ->QWidget
w.show(); // Window object Default will not show Must call show Method display window
return a.exec(); // Let the application object enter the message loop
}
边栏推荐
- Redis configuration file download
- On data security
- Solve the problem of slow batch insertion of MySQL JDBC data
- JS basic knowledge - daily learning summary ①
- 数据提取1
- Stored procedure trial 2 -- establish a test table to test different types of stored procedures
- Massive data Xiao Feng: jointly build and govern opengauss root community and share a thriving new ecosystem
- JS rotation chart
- PHP realizes data interaction with MySQL
- Dasctf2022.07 enabling game password WP
猜你喜欢

Background image related applications - full, adaptive

Data extraction 2

Installation and use of beef XSS

Hundreds of people participated. What are these people talking about in the opengauss open source community?

QT creator code style plug-in beautifier

信息化项目风险控制与应用
![[NPUCTF2020]ReadlezPHP 1](/img/d9/590446b45f917be3f077a9ea739c20.png)
[NPUCTF2020]ReadlezPHP 1

MCDF顶层验证方案

2020 International Machine Translation Competition: Volcano translation won five championships

Fluent rendering mechanism - GPU thread rendering
随机推荐
[MRCTF2020]PYWebsite 1
Breadth first search
"PHP Basics" uses echo statements to output information
Dirsearch[directory scanning tool]
Binglog backup data
"Intermediate and advanced test questions": what is the implementation principle of mvcc?
[netding cup 2020 Qinglong group]areuserialz (buuctf)
1024 | in the fourth year officially called Menon, the original intention is still there, and continue to move forward
pytorch_demo1
Fluent rendering mechanism - GPU thread rendering
On Valentine's day, I drew an object with characters!
After installing mysql, docker entered the container and found that he could not log in to MySQL
ERP production operation control Huaxia
[BJDCTF2020]EasySearch 1
Functions and arrow functions
Have a good laugh
My senior
[golang] golang develops wechat official account web page authorization function
JS basic knowledge - daily learning summary ①
JS rotation chart