Qt工作笔记-QDialog模式对话框传递数据给主窗口
生活随笔
收集整理的這篇文章主要介紹了
Qt工作笔记-QDialog模式对话框传递数据给主窗口
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
話不多說(shuō),上圖:
這里同樣是采用了Qt的信號(hào)與槽機(jī)制。
項(xiàng)目文件分布如下:
代碼如下:
dialog.h
#ifndef DIALOG_H #define DIALOG_H#include <QDialog>namespace Ui { class Dialog; }class Dialog : public QDialog {Q_OBJECT signals:void sendData(QString data);public:explicit Dialog(QWidget *parent = 0);~Dialog();public slots:void pressBtn();private:Ui::Dialog *ui; };#endif // DIALOG_Hwidget.h
#ifndef WIDGET_H #define WIDGET_H#include <QWidget> #include "dialog.h"namespace Ui { class Widget; }class Widget : public QWidget {Q_OBJECTpublic slots:void pressBtn();void updateData(QString data); public:explicit Widget(QWidget *parent = 0);~Widget();private:Ui::Widget *ui;Dialog *dialog; };#endif // WIDGET_Hdialog.cpp
#include "dialog.h" #include "ui_dialog.h"Dialog::Dialog(QWidget *parent) :QDialog(parent),ui(new Ui::Dialog) {ui->setupUi(this);connect(ui->pushButton,SIGNAL(clicked(bool)),this,SLOT(pressBtn())); }void Dialog::pressBtn(){emit sendData(ui->lineEdit->text());this->close(); }Dialog::~Dialog() {delete ui; }widget.cpp
#include "widget.h" #include "ui_widget.h"Widget::Widget(QWidget *parent) :QWidget(parent),ui(new Ui::Widget) {ui->setupUi(this);connect(ui->pushButton,SIGNAL(clicked(bool)),this,SLOT(pressBtn())); }void Widget::updateData(QString data){ui->textEdit->append(data); }void Widget::pressBtn(){dialog=new Dialog();connect(dialog,SIGNAL(sendData(QString)),this,SLOT(updateData(QString)));if(dialog->exec()==QDialog::Accepted){}delete dialog;}Widget::~Widget() {delete ui; }main.cpp
#include "widget.h" #include <QApplication>int main(int argc, char *argv[]) {QApplication a(argc, argv);Widget w;w.show();return a.exec(); }總結(jié)
以上是生活随笔為你收集整理的Qt工作笔记-QDialog模式对话框传递数据给主窗口的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: C++文档阅读笔记-STL中pair的初
- 下一篇: Qt工作笔记-对setupUi(this