标签:
之前的 Qt 编程大多只涉及简单的多窗口,并未染指窗口间的传值交互,想来还是“涉世未深”,对 Qt 的理解、应用还需殷勤努力。
#ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> namespace Ui { class MainWindow; } class MainWindow : public QMainWindow { Q_OBJECT public: explicit MainWindow(QWidget *parent = 0); ~MainWindow(); void setLineText(QString str); private slots: void on_pushButton_clicked(); private: Ui::MainWindow *ui; }; #endif // MAINWINDOW_H |
#include "mainwindow.h" #include "ui_mainwindow.h" #include "dialog.h" #include "ui_dialog.h" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); } MainWindow::~MainWindow() { delete ui; } void MainWindow::on_pushButton_clicked() { Dialog subwin(this); //"this" is very important.
subwin.setGeometry(x()-100, y()+100, wifth()-100, height()-100); subwin.exec(); //needed.
} void MainWindow::setLineText(QString str) { ui->lineEdit->setText(str); } |
#ifndef DIALOG_H #define DIALOG_H #include <QDialog> namespace Ui { class Dialog; } class Dialog : public QDialog { Q_OBJECT public: explicit Dialog(QWidget *parent = 0); ~Dialog(); private slots: void on_pushButton_clicked(); private: Ui::Dialog *ui; }; #endif // DIALOG_H |
#include "dialog.h" #include "ui_dialog.h" #include "mainwindow.h" #include "ui_mainwindow.h" Dialog::Dialog(QWidget *parent) : QDialog(parent), ui(new Ui::Dialog) { ui->setupUi(this); } Dialog::~Dialog() { delete ui; } void Dialog::on_pushButton_clicked() { MainWindow *ptr = (MainWindow*)parentWidget(); //"parentWidget" very important. 获得父部件指针,同时需要类型强转. ptr->setLineText("i can do it, too."); close(); } |
http://blog.csdn.net/jan5_reyn/article/details/38955707
标签:
原文地址:http://www.cnblogs.com/findumars/p/5574280.html