10、QT::QDialogTest操作

前言:

本文操作均为在vs2015+QT5.9.5版本中执行

头文件:.h

头文件:progresstest.h

#pragma once

#include <QDialog>
#include "ui_progresstest.h"

class ProgressTest : public QDialog
{
	Q_OBJECT

public:
	ProgressTest(QWidget *parent = Q_NULLPTR);
	~ProgressTest();
public slots:
    //设置槽接受信号
	void SetPro(int pos);

private:
	Ui::ProgressTest ui;
};

头文件:qdialogtest.h

#pragma once

#include <QtWidgets/QDialog>
#include "ui_qdialogtest.h"

class QDialogTest : public QDialog
{
	Q_OBJECT

public:
	QDialogTest(QWidget *parent = Q_NULLPTR);
public slots:
    void Test();//修改lable文本
private:
	Ui::QDialogTestClass ui;
};

头文件:testpro.h

#pragma once

#include <QObject>
#include <QThread>
class TestPro : public QThread
{
	Q_OBJECT

public:
	TestPro();
	~TestPro();
signals:
	void SetPro(int pos);//创建一个信号
protected:
	void run();//run线程可以设为私密的
};

头文件:xmessagebox.h

#pragma once

#include <QDialog>
#include "ui_xmessagebox.h"

class XMessageBox : public QDialog
{
	Q_OBJECT

public:
	XMessageBox(QWidget *parent = Q_NULLPTR);
	~XMessageBox();
	static int info(QString text);
private:
	Ui::XMessageBox ui;
};

UI文件:ui_.h

UI文件:ui_progresstest.h

/********************************************************************************
** Form generated from reading UI file 'progresstest.ui'
**
** Created by: Qt User Interface Compiler version 5.9.5
**
** WARNING! All changes made in this file will be lost when recompiling UI file!
********************************************************************************/

#ifndef UI_PROGRESSTEST_H
#define UI_PROGRESSTEST_H

#include <QtCore/QVariant>
#include <QtWidgets/QAction>
#include <QtWidgets/QApplication>
#include <QtWidgets/QButtonGroup>
#include <QtWidgets/QDialog>
#include <QtWidgets/QHeaderView>
#include <QtWidgets/QLabel>
#include <QtWidgets/QProgressBar>
#include <QtWidgets/QWidget>

QT_BEGIN_NAMESPACE

class Ui_ProgressTest
{
public:
    QWidget *widget;
    QProgressBar *progressBar;
    QLabel *label;
    QLabel *label_2;

    void setupUi(QDialog *ProgressTest)
    {
        if (ProgressTest->objectName().isEmpty())
            ProgressTest->setObjectName(QStringLiteral("ProgressTest"));
        ProgressTest->resize(560, 220);
        ProgressTest->setStyleSheet(QString::fromUtf8("#widget{\n"
"border-radius:30;/*\350\256\276\347\275\256\345\234\206\350\247\222*/\n"
"background-color: rgb(1, 38, 59);/*\350\256\276\347\275\256\350\203\214\346\231\257\351\242\234\350\211\262*/\n"
"border:0px solid gray;\n"
"}"));
        widget = new QWidget(ProgressTest);
        widget->setObjectName(QStringLiteral("widget"));
        widget->setGeometry(QRect(0, 0, 560, 220));
        progressBar = new QProgressBar(widget);
        progressBar->setObjectName(QStringLiteral("progressBar"));
        progressBar->setGeometry(QRect(50, 100, 471, 21));
        progressBar->setStyleSheet(QString::fromUtf8("font: 12pt \"\351\273\221\344\275\223\";\n"
"color: rgb(255, 255, 255);\n"
""));
        progressBar->setValue(24);
        label = new QLabel(widget);
        label->setObjectName(QStringLiteral("label"));
        label->setGeometry(QRect(240, 40, 61, 21));
        label->setStyleSheet(QStringLiteral("color: rgb(255, 255, 255);"));
        label_2 = new QLabel(widget);
        label_2->setObjectName(QStringLiteral("label_2"));
        label_2->setGeometry(QRect(180, 40, 61, 21));
        label_2->setStyleSheet(QStringLiteral("color: rgb(255, 255, 255);"));

        retranslateUi(ProgressTest);

        QMetaObject::connectSlotsByName(ProgressTest);
    } // setupUi

    void retranslateUi(QDialog *ProgressTest)
    {
        ProgressTest->setWindowTitle(QApplication::translate("ProgressTest", "ProgressTest", Q_NULLPTR));
        label->setText(QString());
        label_2->setText(QApplication::translate("ProgressTest", "\346\270\220\345\217\230\350\277\233\345\272\246\357\274\232", Q_NULLPTR));
    } // retranslateUi

};

namespace Ui {
    class ProgressTest: public Ui_ProgressTest {};
} // namespace Ui

QT_END_NAMESPACE

#endif // UI_PROGRESSTEST_H

UI文件:ui_qdialogtest.h

/********************************************************************************
** Form generated from reading UI file 'qdialogtest.ui'
**
** Created by: Qt User Interface Compiler version 5.9.5
**
** WARNING! All changes made in this file will be lost when recompiling UI file!
********************************************************************************/

#ifndef UI_QDIALOGTEST_H
#define UI_QDIALOGTEST_H

#include <QtCore/QVariant>
#include <QtWidgets/QAction>
#include <QtWidgets/QApplication>
#include <QtWidgets/QButtonGroup>
#include <QtWidgets/QDialog>
#include <QtWidgets/QHeaderView>
#include <QtWidgets/QPushButton>

QT_BEGIN_NAMESPACE

class Ui_QDialogTestClass
{
public:
    QPushButton *pushButton;
    QPushButton *pushButton_2;
    QPushButton *pushButton_3;

    void setupUi(QDialog *QDialogTestClass)
    {
        if (QDialogTestClass->objectName().isEmpty())
            QDialogTestClass->setObjectName(QStringLiteral("QDialogTestClass"));
        QDialogTestClass->resize(1013, 588);
        pushButton = new QPushButton(QDialogTestClass);
        pushButton->setObjectName(QStringLiteral("pushButton"));
        pushButton->setGeometry(QRect(250, 250, 151, 71));
        pushButton_2 = new QPushButton(QDialogTestClass);
        pushButton_2->setObjectName(QStringLiteral("pushButton_2"));
        pushButton_2->setGeometry(QRect(370, 400, 131, 71));
        pushButton_3 = new QPushButton(QDialogTestClass);
        pushButton_3->setObjectName(QStringLiteral("pushButton_3"));
        pushButton_3->setGeometry(QRect(490, 250, 151, 71));

        retranslateUi(QDialogTestClass);
        QObject::connect(pushButton, SIGNAL(clicked()), QDialogTestClass, SLOT(accept()));
        QObject::connect(pushButton_3, SIGNAL(clicked()), QDialogTestClass, SLOT(reject()));
        QObject::connect(pushButton_2, SIGNAL(clicked()), QDialogTestClass, SLOT(Test()));

        QMetaObject::connectSlotsByName(QDialogTestClass);
    } // setupUi

    void retranslateUi(QDialog *QDialogTestClass)
    {
        QDialogTestClass->setWindowTitle(QApplication::translate("QDialogTestClass", "QDialogTest", Q_NULLPTR));
        pushButton->setText(QApplication::translate("QDialogTestClass", "\347\241\256\350\256\244", Q_NULLPTR));
        pushButton_2->setText(QApplication::translate("QDialogTestClass", "Test", Q_NULLPTR));
        pushButton_3->setText(QApplication::translate("QDialogTestClass", "\345\217\226\346\266\210", Q_NULLPTR));
    } // retranslateUi

};

namespace Ui {
    class QDialogTestClass: public Ui_QDialogTestClass {};
} // namespace Ui

QT_END_NAMESPACE

#endif // UI_QDIALOGTEST_H

UI文件:ui_xmessagebox.h

/********************************************************************************
** Form generated from reading UI file 'xmessagebox.ui'
**
** Created by: Qt User Interface Compiler version 5.9.5
**
** WARNING! All changes made in this file will be lost when recompiling UI file!
********************************************************************************/

#ifndef UI_XMESSAGEBOX_H
#define UI_XMESSAGEBOX_H

#include <QtCore/QVariant>
#include <QtWidgets/QAction>
#include <QtWidgets/QApplication>
#include <QtWidgets/QButtonGroup>
#include <QtWidgets/QDialog>
#include <QtWidgets/QHeaderView>
#include <QtWidgets/QLabel>
#include <QtWidgets/QPushButton>
#include <QtWidgets/QWidget>

QT_BEGIN_NAMESPACE

class Ui_XMessageBox
{
public:
    QWidget *backgroundWidget;
    QPushButton *okButton;
    QPushButton *offButton_2;
    QLabel *label;
    QPushButton *pushButton;

    void setupUi(QDialog *XMessageBox)
    {
        if (XMessageBox->objectName().isEmpty())
            XMessageBox->setObjectName(QStringLiteral("XMessageBox"));
        XMessageBox->resize(800, 400);
        XMessageBox->setStyleSheet(QString::fromUtf8("#backgroundWidget{\n"
"border-radius:30;/*\350\256\276\347\275\256\345\234\206\350\247\222*/\n"
"background-color: rgb(209, 209, 209);/*\350\256\276\347\275\256\350\203\214\346\231\257\351\242\234\350\211\262*/\n"
"border:1px solid gray;\n"
"}\n"
"QPushButton{\n"
"background-color: qlineargradient(spread:reflect, x1:1, y1:0.455, x2:1, y2:1, stop:0.380682 rgba(0, 0, 0, 255), stop:1 rgba(255, 255, 255, 255));\n"
"border-radius:10;/*\350\256\276\347\275\256\345\234\206\350\247\222*/\n"
"color:rgb(255,255,255);\n"
"}\n"
""));
        backgroundWidget = new QWidget(XMessageBox);
        backgroundWidget->setObjectName(QStringLiteral("backgroundWidget"));
        backgroundWidget->setGeometry(QRect(0, 0, 800, 400));
        backgroundWidget->setStyleSheet(QString::fromUtf8("#breckgroundWidget{\n"
"border-radius:30;/*\350\256\276\347\275\256\345\234\206\350\247\222*/\n"
"background-color: rgb(209, 209, 209);/*\350\256\276\347\275\256\350\203\214\346\231\257\351\242\234\350\211\262*/\n"
"border:1px solid gray;\n"
"}"));
        okButton = new QPushButton(backgroundWidget);
        okButton->setObjectName(QStringLiteral("okButton"));
        okButton->setGeometry(QRect(210, 260, 131, 61));
        offButton_2 = new QPushButton(backgroundWidget);
        offButton_2->setObjectName(QStringLiteral("offButton_2"));
        offButton_2->setGeometry(QRect(440, 260, 131, 61));
        label = new QLabel(backgroundWidget);
        label->setObjectName(QStringLiteral("label"));
        label->setGeometry(QRect(0, 150, 800, 70));
        label->setStyleSheet(QString::fromUtf8("font: 12pt \"\351\273\221\344\275\223\";"));
        label->setAlignment(Qt::AlignCenter);
        pushButton = new QPushButton(backgroundWidget);
        pushButton->setObjectName(QStringLiteral("pushButton"));
        pushButton->setGeometry(QRect(340, 340, 75, 23));

        retranslateUi(XMessageBox);
        QObject::connect(okButton, SIGNAL(clicked()), XMessageBox, SLOT(accept()));
        QObject::connect(offButton_2, SIGNAL(clicked()), XMessageBox, SLOT(reject()));
        QObject::connect(pushButton, SIGNAL(clicked()), XMessageBox, SLOT(close()));

        QMetaObject::connectSlotsByName(XMessageBox);
    } // setupUi

    void retranslateUi(QDialog *XMessageBox)
    {
        XMessageBox->setWindowTitle(QApplication::translate("XMessageBox", "XMessageBox", Q_NULLPTR));
        okButton->setText(QApplication::translate("XMessageBox", "\347\241\256\350\256\244", Q_NULLPTR));
        offButton_2->setText(QApplication::translate("XMessageBox", "\345\217\226\346\266\210", Q_NULLPTR));
        label->setText(QApplication::translate("XMessageBox", "\346\265\213\350\257\225\344\277\241\346\201\257", Q_NULLPTR));
        pushButton->setText(QApplication::translate("XMessageBox", "\345\205\263\351\227\255", Q_NULLPTR));
    } // retranslateUi

};

namespace Ui {
    class XMessageBox: public Ui_XMessageBox {};
} // namespace Ui

QT_END_NAMESPACE

#endif // UI_XMESSAGEBOX_H

主函数:main.cpp

#include "qdialogtest.h"
#include <QtWidgets/QApplication>
#include <QDebug>
#include "xmessagebox.h"
#include "progresstest.h"
#include "testpro.h"
/******************************************************************

基于:QDialog的函数介绍:
      1、exec():将对话框显示为模态对话框,直到用户关闭它才会停止。
      2、setWindowTitle():修改标题名字。

******************************************************************/

int main(int argc, char *argv[])
{
	QApplication a(argc, argv);

#if 0
	//构造一个窗口对象
	ProgressTest pro;
	//构造一个线程对象
	TestPro tp;
	//自定义线程信号链接槽
	QObject::connect(&tp, SIGNAL(SetPro(int)), &pro, SLOT(SetPro(int)));
	//运行线程,进度条每过一秒加百分之一;
	tp.start();
	pro.exec();
#endif//窗口构造以及线程调用
#if 0
	//手动创建QDialog
	QDialog d;
	d.setWindowTitle("New QDialog");
	int re = d.exec();
	qDebug() << "re=" << re;
	switch (re)
	{
	case QDialog::Accepted:
		qDebug() << "Accepted" ;
		break;
	case QDialog::Rejected:
		qDebug() << "Rejected";
		break;
	default:
		qDebug() << re;
		break;
	}
	//自动创建的QDialogTest
	QDialogTest w;
	int re = w.exec();//获取返回值
	qDebug() << "re=" << re;
	qDebug() << "result=" << w.result();//结果事件,返回值为int
	switch (re)
	{
	case QDialog::Accepted://接受事件,返回值为1
		qDebug() << "Accepted";
		break;
	case QDialog::Rejected://拒绝事件,返回为0
		qDebug() << "Rejected";
		break;
	default:
		qDebug() <<"done="<< re;//完成事件,返回为自定义值
		break;
	}
	w.show();

#endif //QDialog的创建以及exec()事件测试
#if 0
	//自定义QDialog中info事件测试
	qDebug() << XMessageBox::info(QString::fromLocal8Bit("测试"));
#endif//QDialog的窗口显示,以及info事件测试
	return a.exec();
}

实现文件:.cpp

实现文件:progresstest.cpp

#include "progresstest.h"
#include <QThread>
ProgressTest::ProgressTest(QWidget *parent)
{
	ui.setupUi(this);
	//去除标题栏
	this->setWindowFlags(Qt::FramelessWindowHint);
	//设置背景颜色透明
	this->setAttribute(Qt::WA_TranslucentBackground);
	
}

ProgressTest::~ProgressTest()
{
}
//修改进度条进度
void ProgressTest::SetPro(int pos)
{
	//设置进度条进度
	ui.progressBar->setValue(pos);
	if (pos == 100)//判断是否结束,进入渐变消失
	{
		for (int i = 100; i > 0; i--)
		{
			//这里传的是个double类型所有要转换下整形为浮点型
			//设置透明度(浮点型)
			this->setWindowOpacity((double)i/100.0);
			QThread::msleep(10);
			ui.label->setText(QString::number(i));
			//处理所有消息队列的事件
			QEventLoop loop;
			loop.processEvents();//处理事件
		}
		this->close();
	}
}

实现文件:qdialogtest.cpp

#include "qdialogtest.h"

QDialogTest::QDialogTest(QWidget *parent)
	: QDialog(parent)
{
	ui.setupUi(this);
}
void QDialogTest::Test()
{
	done(10);//完成事件,返回为自定义值
}

实现文件:testpro.cpp

#include "testpro.h"
#include <QThread>
TestPro::TestPro()
{

}

TestPro::~TestPro()
{
}

void TestPro::run()
{
	for (int i = 0; i <= 1000; i++)
	{
		SetPro(i);
		msleep(5);//毫秒
	}
}

实现文件:xmessagebox.cpp

#include "testpro.h"
#include <QThread>
TestPro::TestPro()
{

}

TestPro::~TestPro()
{
}

void TestPro::run()
{
	for (int i = 0; i <= 1000; i++)
	{
		SetPro(i);
		msleep(5);//毫秒
	}
}

ui界面布局样式:

progresstest布局样式

在这里插入图片描述

qdialogtest布局样式

在这里插入图片描述

xmessagebox布局样式

在这里插入图片描述

样式表:

progresstest.ui

#widget{/*限定名称为widget的控件修改样式*/
border-radius:30;/*设置圆角*/
background-color: rgb(1, 38, 59);/*设置背景颜色*/
border:0px solid gray;
}
QLabel{/*设置字体*/
color: rgb(255, 255, 255);
}
QProgressBar{/*设置字体及其颜色*/
font: 12pt "黑体";
color: rgb(255, 255, 255);

}

qdialogtest.ui

未设置样式表!

xmessagebox.ui

#backgroundWidget{
border-radius:30;/*设置圆角*/
background-color: rgb(209, 209, 209);/*设置背景颜色*/
border:1px solid gray;
}
QPushButton{
background-color: qlineargradient(spread:reflect, x1:1, y1:0.455, x2:1, y2:1, stop:0.380682 rgba(0, 0, 0, 255), stop:1 rgba(255, 255, 255, 255));
border-radius:10;/*设置圆角*/
color:rgb(255,255,255);
}
QLabel{
font: 12pt "黑体";
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

柳一航

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值