好久没写过Qt了,写个Qt回味一下信号与槽
界面效果
界面类.h
#include <QtWidgets/qmainwindow.h>
#include <QtWidgets/qwidget.h>
#include <QtWidgets/qplaintextedit.h>
#include <QtWidgets/qboxlayout.h>
#include <QtWidgets/qlineedit.h>
#include <QtWidgets/qpushbutton.h>
//#pragma comment(lib,"Qt5Widgetsd.lib")
class testClass : public QMainWindow
{
Q_OBJECT
public:
testClass(QWidget *parent = nullptr);
~testClass();
void initWindow();
protected slots:
void changePainText(); //槽函数
private:
QPlainTextEdit* m_pPainTex;
QLineEdit* m_pLineEdit;
QPushButton* m_pButton;
};
界面实现.cpp
#include "testClass.h"
testClass::testClass(QWidget *parent): QMainWindow(parent)
{
initWindow();
connect(m_pButton, SIGNAL(released()),this,SLOT(changePainText())); //槽函数
this->setWindowTitle("Testing");
this->setMinimumSize(450,250);
}
void testClass::changePainText()
{
if (m_pLineEdit)
{
QString strLineEdit = m_pLineEdit->text();
if (m_pPainTex && !strLineEdit.isEmpty())
{
m_pPainTex->setPlainText(strLineEdit);
}
}
return;
}
void testClass::initWindow()
{
m_pPainTex = new QPlainTextEdit(this);
m_pPainTex->setGeometry(50,25,350,100); //富文本的布局位置
m_pPainTex->setReadOnly(true); //设置只读模式
m_pLineEdit = new QLineEdit(this); //设置父,用于半自动化释放
m_pLineEdit->setGeometry(50,145,50,25);
m_pButton = new QPushButton(this);
m_pButton->setGeometry(350,145,50,25);
m_pButton->setText("click");
}
testClass::~testClass()
{
if (m_pPainTex)
{
delete m_pPainTex;
m_pPainTex = nullptr;
}
if (m_pButton)
{
delete m_pButton;
m_pButton = nullptr;
}
if (m_pLineEdit)
{
delete m_pLineEdit;
m_pLineEdit = nullptr;
}
}
启动窗体.cpp
#include <QtWidgets/qapplication.h>
#include "testClass.h"
int main(int argc, char* argv[])
{
QApplication a(argc, argv);
testClass T;
T.show();
return a.exec();
}