功能简介:
点击qt界面中的open按钮在界面中显示文本,同时按钮变为close按钮,再点击close按钮可以关闭显示文本,同时按钮变为open按钮,继续点击open按钮。。。。。。
关键内容:
// 关联按钮按事件和信号识别槽
connect(this->displayCtrlBtn, &QPushButton::clicked, this, &HelloWorld::DisplayControl);
1.创建一个qt工程
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
CONFIG += c++11
# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS
# You can also make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
SOURCES += \
main.cpp \
helloworld.cpp
HEADERS += \
helloworld.h
FORMS += \
helloworld.ui
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
2.定义一个QPuschButton按钮实例和一个QLabel文本显示实例
#ifndef HELLOWORLD_H
#define HELLOWORLD_H
#include <QWidget>
#include <QPushButton>
#include <QLabel>
QT_BEGIN_NAMESPACE
namespace Ui { class HelloWorld; }
QT_END_NAMESPACE
class HelloWorld : public QWidget
{
Q_OBJECT
public:
HelloWorld(QWidget *parent = nullptr);
~HelloWorld();
void DisplayControl();
private slots:
private:
Ui::HelloWorld *ui;
QPushButton *displayCtrlBtn;
QLabel *helloWroldLabel;
bool labelStatus;
};
#endif // HELLOWORLD_H
3.设置按钮显示内容和文本框显示内容, 关联按钮按事件和信号识别槽
#include "helloworld.h"
#include "ui_helloworld.h"
#include <QDebug>
#include <QPushButton>
HelloWorld::HelloWorld(QWidget *parent)
: QWidget(parent)
, ui(new Ui::HelloWorld)
{
ui->setupUi(this);
labelStatus = true;
displayCtrlBtn = new QPushButton;
displayCtrlBtn->setParent(this);//将按钮挂载到窗口上
displayCtrlBtn->setText("Open");
displayCtrlBtn->move(250, 250);
helloWroldLabel = new QLabel(this);
this->helloWroldLabel->setText("hello world!");
// 设置坐标和大小
helloWroldLabel->setGeometry(180, 50, 400, 200);
// 设置字号大小
QFont ft;
ft.setPointSize(18);
helloWroldLabel->setFont(ft);
// 设置颜色
QPalette pale;
pale.setColor(QPalette::WindowText, Qt::red);
helloWroldLabel->setPalette(pale);
helloWroldLabel->hide();
// 关联按钮按事件和信号识别槽
connect(this->displayCtrlBtn, &QPushButton::clicked, this, &HelloWorld::DisplayControl);
}
HelloWorld::~HelloWorld()
{
delete ui;
}
void HelloWorld::DisplayControl()
{
qDebug() << "my_btn_callback ! " << endl;
if (labelStatus) {
helloWroldLabel->hide();
this->displayCtrlBtn->setText("Open");
labelStatus = false;
} else {
helloWroldLabel->show();
this->displayCtrlBtn->setText("close");
labelStatus = true;
}
}
4.main函数中添加测试
#include "helloworld.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
HelloWorld w;
w.show();
return a.exec();
}
5.运行效果
打开时界面中没有显示文本,需要点击open按钮显示文本内容文章来源:https://www.toymoban.com/news/detail-519447.html
点击open按钮显示文本,同时open按钮变为close按钮,再点击close按钮关闭显示文本以及修改按钮状态文章来源地址https://www.toymoban.com/news/detail-519447.html
到了这里,关于Qt信号和槽绑定实例,点击pushbutton按钮触发QLabel文本显示和关闭的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!