标签:text obj sig signal [] .exe gre label utf-8
//mydialog.h #ifndef MYDIALOG_H #define MYDIALOG_H #include <QDialog> namespace Ui { class MyDialog; } class MyDialog : public QDialog { Q_OBJECT public: explicit MyDialog(QWidget *parent = 0); ~MyDialog(); private slots: void on_pushButton_clicked(); signals: void dlgReturn(int); //自定义的信号 private: Ui::MyDialog *ui; }; #endif // MYDIALOG_H
//widget.h #ifndef WIDGET_H #define WIDGET_H #include <QWidget> namespace Ui { class Widget; } class Widget : public QWidget { Q_OBJECT public: explicit Widget(QWidget *parent = 0); ~Widget(); private slots: void showValue(int value); private: Ui::Widget *ui; }; #endif // WIDGET_H
//mydialog.cpp #include "mydialog.h" #include "ui_mydialog.h" MyDialog::MyDialog(QWidget *parent) : QDialog(parent), ui(new Ui::MyDialog) { ui->setupUi(this); } MyDialog::~MyDialog() { delete ui; } // 确定按钮 void MyDialog::on_pushButton_clicked() { // 获取输入的数值 int value = ui->spinBox->value(); // 发射信号 emit dlgReturn(value); // 关闭对话框 close(); }
//widget.cpp #include "widget.h" #include "ui_widget.h" #include "mydialog.h" Widget::Widget(QWidget *parent) : QWidget(parent), ui(new Ui::Widget) { ui->setupUi(this); // 创建对话框 MyDialog *dlg = new MyDialog(this); // 将对话框中的自定义信号与主界面中的自定义槽函数进行关联 connect(dlg,SIGNAL(dlgReturn(int)),this,SLOT(showValue(int))); // 显示对话框 dlg->show(); } Widget::~Widget() { delete ui; } // 自定义槽函数 void Widget::showValue(int value) { ui->label->setText(tr("获取的值是:%1").arg(value)); }
//main.cpp #include <QApplication> #include "widget.h" #include <QTextCodec> int main(int argc, char *argv[]) { QApplication a(argc, argv); //QTextCodec::setCodecForTr(QTextCodec::codecForName("UTF-8")); Widget w; w.show(); return a.exec(); }
标签:text obj sig signal [] .exe gre label utf-8
原文地址:http://www.cnblogs.com/qrxqrx/p/6918515.html