日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

Qt開發實現跨窗口信號槽通信_C 語言

作者:踏莎行hyx ? 更新時間: 2022-03-24 編程語言

多窗口通信,如果是窗口類對象之間互相包含,則可以直接開放public接口調用,不過,很多情況下主窗口和子窗口之間要做到異步消息通信,就必須依賴到跨窗口的信號槽,以下是一個簡單的示例。

母窗口

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QLabel>
#include <QString>

class MainWindow : public QMainWindow
{
? ? Q_OBJECT

public:
? ? MainWindow(QWidget *parent = 0);
? ? ~MainWindow();

private slots:
? ? void receiveMsg(QString str);

private:
? ? QLabel *label;
};

#endif // MAINWINDOW_H

mainwindow.cpp

#include "mainwindow.h"
#include "subwindow.h"

MainWindow::MainWindow(QWidget *parent)
? ? : QMainWindow(parent)
{
? ? setWindowTitle("MainWindow");
? ? setFixedSize(400, 300);

? ? // add text label
? ? label = new QLabel(this);
? ? label->setText("to be changed");

? ? // open sub window and connect
? ? SubWindow *subwindow = new SubWindow(this);
? ? connect(subwindow, SIGNAL(sendText(QString)), this, SLOT(receiveMsg(QString)));
? ? subwindow->show(); // use open or exec both ok
}

void MainWindow::receiveMsg(QString str)
{
? ? // receive msg in the slot
? ? label->setText(str);
}

MainWindow::~MainWindow()
{
}

子窗口

subwindow.h

#ifndef SUBWINDOW_H
#define SUBWINDOW_H

#include <QDialog>

class SubWindow : public QDialog
{
? ? Q_OBJECT
public:
? ? explicit SubWindow(QWidget *parent = 0);

signals:
? ? void sendText(QString str);
public slots:
? ? void onBtnClick();
};

#endif // SUBWINDOW_H

subwindow.cpp

#include "QPushButton"
#include "subwindow.h"

SubWindow::SubWindow(QWidget *parent) : QDialog(parent)
{
? ? setWindowTitle("SubWindow");
? ? setFixedSize(200, 100);

? ? QPushButton *button = new QPushButton("click", this);
? ? connect(button, SIGNAL(clicked()), this, SLOT(onBtnClick()));
}

void SubWindow::onBtnClick()
{
? ? // send signal
? ? emit sendText("hello qt");
}

截圖:

基本思路:

1、子窗口發送信號

2、主窗口打開子窗口,并創建好信號槽關聯

3、通過信號槽函數傳遞消息參數

原文鏈接:https://tashaxing.blog.csdn.net/article/details/83387269

欄目分類
最近更新