【QT】父组件与子组件的相互通信例子(用signal-slot实现)

目标

新建一个QMainWindow窗口,在该窗口中添加一个打开按钮,一个spinBox,点击打开按钮后弹出一个对话框,对话框里面有一个slider。改变slider后spinBox的值会改变,改变spinBox后slider的值会改变,变动值范围:0-130.

实现后是下图这样的:

代码在:【QT】父组件与子组件的相互通信例子(用signal-slot实现)

环境:

  • win7 64
  • vs2013
  • qt5.6.0 32
  • qt add in 1.2.5

工程中有如下文件:

  • hello.h
  • Mydialog.h
  • hello.cpp
  • Mydialog.cpp
  • main.cpp

每个文件的内容

代码在:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
hello.h
#include <QtWidgets/QMainWindow>
#include "ui_hello.h"
#include "Mydialog.h"
#include <qspinbox.h>
class Hello : public QMainWindow
{
Q_OBJECT
public:
Hello(QWidget *parent = 0);
~Hello();
signals:
void sendMsg2(int value);
private:
Ui::HelloClass ui;
// 添加
void open(); //打开dialog
void setSpinBox(int value);
QAction *openAction;
Mydialog *dialog;
QSpinBox *spinBox;
};
#endif // HELLO_H
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
//Mydialog.h
#pragma once
#include <QDialog>
#include <qslider.h>
class Mydialog : public QDialog
{
Q_OBJECT
public:
Mydialog(QWidget *parent);
~Mydialog();
signals:
void sendMsg(int value);
private:
QSlider *slider;
};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
//hello.cpp
#include "hello.h"
#include <qaction.h> // ..
#include <qmenubar.h>
#include <qmessagebox.h>
#include <qstatusbar.h>
#include <qtoolbar.h>
#include <qdebug.h>
#include <qspinbox.h>
#include <qslider.h>
#include <qboxlayout.h>
Hello::Hello(QWidget *parent)
: QMainWindow(parent)
{
ui.setupUi(this);
setWindowTitle(tr("Main Window by tsc 292936085#qq.com")); //设置主窗口的标题
openAction = new QAction(QIcon("./doc-open.png"), tr("&Open..."), this);//icon, text, parent
openAction->setShortcut(QKeySequence::Open); //Open的快捷键
openAction->setStatusTip(tr("Open an existing file-from tsc")); //状态栏显示
connect(openAction, &QAction::triggered, this, &Hello::open); //将打开对话框的信号与槽
QMenu *file = menuBar()->addMenu(tr("&File")); //menuBar() 是 QMainWindows 的一个函数
file->addAction(openAction);
QToolBar *toolBar = addToolBar(tr("&File"));
toolBar->addAction(openAction);
spinBox = new QSpinBox(this);
spinBox->setRange(0, 130); //设置范围
QHBoxLayout *layout = new QHBoxLayout;//布局管理器
layout->addWidget(spinBox); //将一个控件添加到一个布局中去
QWidget *centerWindow = new QWidget; //需要用new创建在堆上,不能创建在栈上
this->setCentralWidget(centerWindow); //给mainwindow指定CentralWidget
centerWindow->setLayout(layout); //给CentralWidget设置布局管理器
}
Hello::~Hello()
{
}
void Hello::open()
{
dialog = new Mydialog(this);
dialog->setAttribute(Qt::WA_DeleteOnClose); //对话框关闭时,自动销毁对话框
dialog->setWindowTitle(tr("hello, tsc"));
dialog->show();
connect(dialog, &Mydialog::sendMsg, spinBox, &QSpinBox::setValue); //对话框发送信号过来,spinBox响应
void(QSpinBox:: *spinBoxSignal)(int) = &QSpinBox::valueChanged;
connect(spinBox, spinBoxSignal, dialog, &Mydialog::sendMsg); //sinBox如果改变了,发信号
}
void Hello::setSpinBox(int value)
{
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
//Mydialog.cpp
#include "Mydialog.h"
#include <qboxlayout.h>
Mydialog::Mydialog(QWidget *parent)
: QDialog(parent)
{
slider = new QSlider(Qt::Horizontal, this); //
slider->setRange(0, 130);
QHBoxLayout *layout = new QHBoxLayout; //布局管理器
layout->addWidget(slider);
this->setLayout(layout);//dialog不需要centralWidget
connect(slider, &QSlider::valueChanged, this, &Mydialog::sendMsg); //slider如果改变,发信号
connect(this, &Mydialog::sendMsg, slider, &QSlider::setValue); //接收信号,设置slider的值
}
Mydialog::~Mydialog()
{
}
1
2
3
4
5
6
7
8
9
10
11
12
//main.cpp
#include "hello.h"
#include <QtWidgets/QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Hello w;
w.show();
return a.exec();
}

注意

在mainwindow中添加控件时mainwindow必须要有个centralWidget

Note: Creating a main window without a central widget is not supported. You must have a central widget even if it is just a placeholder.

参考:在QMainWindow添加控件,无法显示问题