一、 VS2015下用Qt Designer创建对话框
文件另存为对象名称一样的liutest.ui文件
菜单项:窗体->查看代码,另存为ui_liutest.h文件
将ui_liutest.h加入到工程中
创建liutest.cpp和liutest.h文件,加入工程。(也可以在qt creator中创建)
liutest.h文件模板:
#pragma once
#include
#include “ui_liutest.h”
namespace Ui {
class liutest;
}
class liutest : public QDialog
{
Q_OBJECT
public:
liutest(QWidget *parent = Q_NULLPTR);
~liutest();
void SetLabel(QString str);
private:
Ui::liutest *ui;
};
xxx.cpp文件模板:
#include “liutest.h”
#include “ui_liutest.h”
liutest::liutest(QWidget *parent)
: QDialog(parent),
ui(new Ui::liutest)
{
ui->setupUi(this);
}
liutest::~liutest()
{
}
void liutest::SetLabel(QString str)
{
ui->label->setText(str);
}PS:
也可以将liutest.ui文件直接添加到工程中,替代添加ui_liutest.h文件,编译的时候会自动生成ui_liutest.h文件
PS:
Dialog返回值有两种: QDialog::Accepted和QDialog::Rejected,点击OK按键关闭返回前者,cancel和直接关闭返回或者
一下为cpp修改内容:
liutt->SetLabel(QString::fromLocal8Bit(str.c_str())); 用于设置其中的值
liutt = new liutest(this); ///新建一个对象
liutt->show(); ///显示界面
int ret = liutt->exec(); ///调用返回参数
if (ret == QDialog::Accepted)
{
printf(“ok\n”);
}
else if (ret == QDialog::Rejected)
{
printf(“no\n”);
}
else
{
printf(“what?\n”);
}
#ifndef MYFORM_H
#define MYFORM_H
#include
namespace Ui {
class myForm;
}
class myForm : public QDialog
{
Q_OBJECT
public:
explicit myForm(QWidget *parent = 0);
~myForm();
signals:
void sendData(QString); // 用来传输数据的信号
private slots:
void on_pushButton_clicked(); // 按键点击
private:
Ui::myForm *ui;
};
#endif // MYFORM_H
myForm.cpp
#include “myform.h”
#include “ui_myform.h”
myForm::myForm(QWidget *parent) :
QDialog(parent),
ui(new Ui::myForm)
{
ui->setupUi(this);
}
myForm::~myForm()
{
delete ui;
}
void myForm::on_pushButton_clicked()
{
emit sendData(ui->lineEdit->text()); //获取lineEdit的输入并且传递出去
this->close(); // 传输完毕之后关闭子窗口
}
MainWindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include
#include “logindlg.h”
#include
#include
#include
#include “myform.h”
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private slots:
void on_pushButton_clicked();
void on_pushButton_2_clicked();
void receiveData(QString data); //接收传递过来的数据的槽
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
MainWindow.cpp
#include “mainwindow.h”
#include “ui_mainwindow.h”
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
7. MessageBox使用:
void MainWindow::on_pushButton_clicked()
{
QMessageBox msgBox;
msgBox.setText(tr(“The document has been modified.”));
msgBox.setInformativeText(tr(“Do you want to save your changes?”));
msgBox.setDetailedText(tr(“Differences here…”));
msgBox.setStandardButtons(QMessageBox::Save
| QMessageBox::Discard
| QMessageBox::Cancel);
msgBox.setDefaultButton(QMessageBox::Save);
int ret = msgBox.exec();
switch (ret) {
case QMessageBox::Save:
qDebug() << “Save document!”;
break;
case QMessageBox::Discard:
qDebug() << “Discard changes!”;
break;
case QMessageBox::Cancel:
qDebug() << “Close document!”;
break;
}
}
void MainWindow::on_pushButton_2_clicked()
{
myForm *form = new myForm(this);
// 连接信号与槽
connect(form, SIGNAL(sendData(QString)), this, SLOT(receiveData(QString)));
form->show();
}
void MainWindow::receiveData(QString data)
{
ui->textEdit->setText(data);
}
7. MessageBox使用:
1、
void MainWindow::on_pushButton_clicked()
{
// QMessageBox *dlg = new QMessageBox(this);
if (QMessageBox::Yes == QMessageBox::question(this,
tr(“Question”),
tr(“Are you OK?”),
QMessageBox::Yes | QMessageBox::No,
QMessageBox::Yes)) {
QMessageBox::information(this, tr(“Hmmm…”), tr(“I’m glad to hear that!”));
} else {
QMessageBox::information(this, tr(“Hmmm…”), tr(“I’m sorry!”));
}
}
2、
{
QMessageBox msgBox;
msgBox.setText(tr(“The document has been modified.”));
msgBox.setInformativeText(tr(“Do you want to save your changes?”));
msgBox.setDetailedText(tr(“Differences here…”));
msgBox.setStandardButtons(QMessageBox::Save
| QMessageBox::Discard
| QMessageBox::Cancel);
msgBox.setDefaultButton(QMessageBox::Save);
int ret = msgBox.exec();
switch (ret) {
case QMessageBox::Save:
qDebug() << “Save document!”;
break;
case QMessageBox::Discard:
qDebug() << “Discard changes!”;
break;
case QMessageBox::Cancel:
qDebug() << “Close document!”;
break;
}
二、 使用为QT界面添加命令