VSQt共同開発例1

1. VSQtでシグナルスロットを使用する方法

①ヘッダーファイルのスロット機能を次のように定義します。

private slots:
    void testBtnClicked();

②コントロールを.uiファイルにドラッグするか、F3ショートカットキーをクリックするか、下のアイコンをクリックして信号スロットを編集します。

③「テスト」ボタンをクリックして、信号とスロットを編集します。

④赤いマークをtestBtnClicked()に変更します。これは、ヘッダーファイルで定義されているスロット関数の名前と一致しています。

⑤.cppファイルにスロット機能を実装する

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

2.信号スロットを使用する2番目の方法

接続機能を直接使用して信号とスロットを接続します。この方法は、QtCreatorで使用される方法と同じです。

hファイル

#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ファイル

#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");
}

 

おすすめ

転載: blog.csdn.net/weixin_41882459/article/details/113730136