VS Qt joint development example 1

1. How to use the signal slot in VS Qt

①Define the slot function in the header file as follows:

private slots:
    void testBtnClicked();

②Drag the control into the .ui file, click the F3 shortcut key, or click the icon below to edit the signal slot.

③Click the "test" button to edit the signals and slots.

④ Change the red mark to testBtnClicked(), which is consistent with the name of the slot function defined in the header file.

⑤Implement the slot function in the .cpp file

void frmMain::testBtnClicked()
{
	QMessageBox *msg = new QMessageBox;
	msg->aboutQt(this, "about qt");
}

2. The second method of using the signal slot

Directly use the connect function to connect signals and slots. This method is the same as that used in Qt Creator.

h file

#pragma once

#include <QtWidgets/QMainWindow>

#include "ui_frmMain.h"

class frmMain : public QMainWindow
{
    Q_OBJECT

public:
    frmMain(QWidget *parent = Q_NULLPTR);


private slots:
	void testBtnClicked();
	void slotBtn1();
	void slotBtn2();

private:
    Ui::frmMainClass ui;
};

cpp file

#pragma execution_character_set("utf-8")
#include "frmMain.h"
#include<QMessageBox>
#include<QDebug>
#include<QPropertyAnimation>
#include<iostream>
#include<QPushButton>
#include<QGridLayout>
frmMain::frmMain(QWidget *parent)
	: QMainWindow(parent)
{
	ui.setupUi(this);
	setWindowTitle(" 测试 程序 ");
	setFixedSize(QSize(300, 200));	
	QPushButton *btn1 = new QPushButton("btn1",this);
	btn1->move(0, 100);	
	QPushButton *btn2 = new QPushButton("btn2",this);
	btn2->move(100, 100);
	connect(btn1, SIGNAL(clicked()), this, SLOT(slotBtn1()));
	connect(btn2, SIGNAL(clicked()), this, SLOT(slotBtn2()));
	QGridLayout *layout = new QGridLayout;
	layout->addWidget(btn1);
	layout->addWidget(btn2);
	this->setLayout(layout);	

}

void frmMain::testBtnClicked()
{
	QMessageBox *msg = new QMessageBox;
	msg->aboutQt(this, "about qt");
}

void frmMain::slotBtn1()
{
	QMessageBox *msg = new QMessageBox;
	msg->about(this, "btn1 clicked", "btn1");
}

void frmMain::slotBtn2()
{
	QMessageBox *msg = new QMessageBox;
	msg->about(this, "btn2 clicked", "btn2");
}

 

Guess you like

Origin blog.csdn.net/weixin_41882459/article/details/113730136