QDialog
1.常用方法
2.QEventLoop 手动处理事件
3.QProgressBar 进度条
QDialog 常用方法
1.exec阻塞有返回值show
2.accept() QDialog::Accepted
3.reject() QDialog::Rejected
4.done(int i)
5.result()
QDialog示例
1.自定义QMessageBox
2.项目中新曾UI
ui_dialog.h
#ifndef UI_DIALOG_H
#define UI_DIALOG_H
#include <QtCore/QVariant>
#include <QtWidgets/QApplication>
#include <QtWidgets/QDialog>
#include <QtWidgets/QPushButton>
QT_BEGIN_NAMESPACE
class Ui_Dialog
{
public:
QPushButton *okButton;
QPushButton *CancelButton;
QPushButton *testButton;
void setupUi(QDialog *Dialog)
{
if (Dialog->objectName().isEmpty())
Dialog->setObjectName(QString::fromUtf8("Dialog"));
Dialog->resize(800, 600);
okButton = new QPushButton(Dialog);
okButton->setObjectName(QString::fromUtf8("okButton"));
okButton->setGeometry(QRect(60, 260, 141, 81));
CancelButton = new QPushButton(Dialog);
CancelButton->setObjectName(QString::fromUtf8("CancelButton"));
CancelButton->setGeometry(QRect(300, 260, 141, 81));
testButton = new QPushButton(Dialog);
testButton->setObjectName(QString::fromUtf8("testButton"));
testButton->setGeometry(QRect(200, 120, 141, 81));
retranslateUi(Dialog);
QObject::connect(okButton, &QPushButton::clicked, Dialog, qOverload<>(&QDialog::accept));
QObject::connect(CancelButton, &QPushButton::clicked, Dialog, qOverload<>(&QDialog::reject));
QObject::connect(testButton, SIGNAL(clicked()), Dialog, SLOT(Test()));
QMetaObject::connectSlotsByName(Dialog);
} // setupUi
void retranslateUi(QDialog *Dialog)
{
Dialog->setWindowTitle(QCoreApplication::translate("Dialog", "Dialog", nullptr));
okButton->setText(QCoreApplication::translate("Dialog", "\347\241\256\350\256\244", nullptr));
CancelButton->setText(QCoreApplication::translate("Dialog", "\345\217\226\346\266\210", nullptr));
testButton->setText(QCoreApplication::translate("Dialog", "test", nullptr));
} // retranslateUi
};
namespace Ui {
class Dialog: public Ui_Dialog {};
} // namespace Ui
QT_END_NAMESPACE
#endif // UI_DIALOG_H
ui_xmessagebox.h
#ifndef UI_XMESSAGEBOX_H
#define UI_XMESSAGEBOX_H
#include <QtCore/QVariant>
#include <QtWidgets/QApplication>
#include <QtWidgets/QDialog>
#include <QtWidgets/QLabel>
#include <QtWidgets/QPushButton>
#include <QtWidgets/QWidget>
QT_BEGIN_NAMESPACE
class Ui_XMessageBox
{
public:
QWidget *widget;
QLabel *label;
QPushButton *okButton;
QPushButton *CancelButton;
void setupUi(QDialog *XMessageBox)
{
if (XMessageBox->objectName().isEmpty())
XMessageBox->setObjectName(QString::fromUtf8("XMessageBox"));
XMessageBox->resize(547, 414);
XMessageBox->setStyleSheet(QString::fromUtf8("#widget{\n"
" border-radius:30;\n"
" background-color: rgb(85, 87, 83);\n"
" border: 1px solid gray;\n"
"}\n"
"\n"
"QPushButton{\n"
" background-color: qlineargradient(spread:reflect, x1:1, y1:0.608, x2:1, y2:1, stop:0 rgba(0, 0, 0, 255), stop:1 rgba(255, 255, 255, 255));\n"
" border-radius:10;\n"
" color: rgb(164, 0, 0);\n"
" \n"
" font: 19pt \"Ubuntu\";\n"
"}\n"
"\n"
"QDialog{\n"
" border-radius:20;\n"
" background-color: rgb(85, 87, 83);\n"
" border:1px solid gray;\n"
"}"));
widget = new QWidget(XMessageBox);
widget->setObjectName(QString::fromUtf8("widget"));
widget->setGeometry(QRect(0, 0, 547, 414));
label = new QLabel(widget);
label->setObjectName(QString::fromUtf8("label"));
label->setGeometry(QRect(-20, 0, 547, 181));
label->setStyleSheet(QString::fromUtf8("font: 700 36pt \"Ubuntu Condensed\";"));
label->setAlignment(Qt::AlignCenter);
okButton = new QPushButton(widget);
okButton->setObjectName(QString::fromUtf8("okButton"));
okButton->setGeometry(QRect(60, 280, 141, 81));
CancelButton = new QPushButton(widget);
CancelButton->setObjectName(QString::fromUtf8("CancelButton"));
CancelButton->setGeometry(QRect(300, 280, 141, 81));
retranslateUi(XMessageBox);
QObject::connect(CancelButton, &QPushButton::clicked, XMessageBox, qOverload<>(&QDialog::reject));
QObject::connect(okButton, &QPushButton::clicked, XMessageBox, qOverload<>(&QDialog::accept));
QMetaObject::connectSlotsByName(XMessageBox);
} // setupUi
void retranslateUi(QDialog *XMessageBox)
{
XMessageBox->setWindowTitle(QCoreApplication::translate("XMessageBox", "XMessageBox", nullptr));
label->setText(QCoreApplication::translate("XMessageBox", "\346\265\213\350\257\225\346\266\210\346\201\257", nullptr));
okButton->setText(QCoreApplication::translate("XMessageBox", "\347\241\256\350\256\244", nullptr));
CancelButton->setText(QCoreApplication::translate("XMessageBox", "\345\217\226\346\266\210", nullptr));
} // retranslateUi
};
namespace Ui {
class XMessageBox: public Ui_XMessageBox {};
} // namespace Ui
QT_END_NAMESPACE
#endif // UI_XMESSAGEBOX_H
xmessagebox.cpp
#include "xmessagebox.h"
#include "ui_xmessagebox.h"
XMessageBox::XMessageBox(QWidget *parent)
: QDialog(parent)
, ui(new Ui::XMessageBox)
{
ui->setupUi(this);
//去掉标题栏
this->setWindowFlags(Qt::FramelessWindowHint);
//设置背景透明
this->setAttribute(Qt::WA_TranslucentBackground,true);
}
void XMessageBox::Test()
{
done(10);
}
XMessageBox::~XMessageBox()
{
delete ui;
}
int XMessageBox::info(QString txt)
{
XMessageBox box;
box.ui->label->setText(txt);
return box.exec();
}