吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 2334|回复: 19
收起左侧

[讨论] Qt基础

  [复制链接]
古月不傲 发表于 2020-8-20 08:12
本帖最后由 古月不傲 于 2020-8-20 08:20 编辑

[C++] 纯文本查看 复制代码
//头文件
#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QWizard>

QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE

class Widget : public QWidget
{
	Q_OBJECT

public:
	explicit Widget(QWidget *parent = nullptr);
	~Widget();

private slots:
	void on_pushButton_color_clicked();

	void on_pushButton_file_clicked();

	void on_pushButton_font_clicked();

	void on_pushButton_input_clicked();

	void on_pushButton_msg_clicked();

	void on_pushButton_progress_clicked();

	void on_pushButton_error_clicked();

	void on_pushButton_guide_clicked();

private:
	Ui::Widget *ui;

	QWizardPage *createPage1(QString strTitle);
	QWizardPage *createPage2(QString strTitle);
	QWizardPage *createPage3(QString strTitle);
private:
	QWizard *m_pQWizard;
};
#endif // WIDGET_H

//源文件
#include "widget.h"
#include "ui_widget.h"
#include <QColorDialog>
#include <QDebug>
#include <QFileDialog>
#include <QFontDialog>
#include <QInputDialog>
#include <QMessageBox>
#include <QProgressDialog>
#include <QCoreApplication>
#include <QErrorMessage>


Widget::Widget(QWidget *parent)
	: QWidget(parent)
	, ui(new Ui::Widget)
	, m_pQWizard(new QWizard(this))
{
	ui->setupUi(this);
}

Widget::~Widget()
{
	delete ui;
}

//设置颜色对话框
void Widget::on_pushButton_color_clicked()
{
	QColor color = QColorDialog::getColor(Qt::red, this, QStringLiteral("颜色对话框"), QColorDialog::ShowAlphaChannel);

	qDebug() << "color:" << color;
}

//设置文件对话框
void Widget::on_pushButton_file_clicked()
{
	QString strFile = QFileDialog::getOpenFileName(this, QStringLiteral("文件对话框"), NULL, QStringLiteral("文本文件(*txt);;系统文件(*dll *sys)"));

	qDebug() << "fileName:" << strFile;
}

//设置字体对话框
void Widget::on_pushButton_font_clicked()
{
	bool ok;

	QFont font = QFontDialog::getFont(&ok, this);

	if (ok)
		ui->pushButton_font->setFont(font);
	else
		qDebug() << QStringLiteral("没有选中字体");
}

//输入对话框
void Widget::on_pushButton_input_clicked()
{
	bool ok;

	//输入字符串
	QString strInput = QInputDialog::getText(this, QStringLiteral("输入对话框"), QStringLiteral("请输入字符串:"),
		QLineEdit::Normal, QStringLiteral("Admin"), &ok);
	if (ok)
		qDebug() << "strInput:" << strInput;

	//输入整数
	int nInput = QInputDialog::getInt(this, QStringLiteral("输入对话框"), QStringLiteral("请输入整数:"),
		250, 0, 1000, 100, &ok);
	if (ok)
		qDebug() << "nInput:" << nInput;

	//输入小数
	double lfInput = QInputDialog::getDouble(this, QStringLiteral("输入对话框"), QStringLiteral("请输入小数:"),
		250, 0, 1000, 100, &ok);
	if (ok)
		qDebug() << "lfInput:" << lfInput;

	QStringList items;
	//输入条目数
	QString strItems = QInputDialog::getItem(this, QStringLiteral("输入对话框"), QStringLiteral("请输入条目数:"),
		items, 0, true, &ok);
	if (ok)
		qDebug() << "strItems:" << strItems;
}

//消息对话框
void Widget::on_pushButton_msg_clicked()
{
	int ret;

	ret = QMessageBox::question(this, QStringLiteral("问题对话框"), QStringLiteral("你了解 Qt吗?"));
	if (ret == QMessageBox::Yes)
		qDebug() << QStringLiteral("问题");
	ret = QMessageBox::information(this, QStringLiteral("提示对话框"), QStringLiteral("这是Qt的书籍"));
	if (ret == QMessageBox::Ok)
		qDebug() << QStringLiteral("提示");
	ret = QMessageBox::warning(this, QStringLiteral("警告对话框"), QStringLiteral("发出警告"));
	if (ret == QMessageBox::Ok)
		qDebug() << QStringLiteral("警告");
	ret = QMessageBox::critical(this, QStringLiteral("严重错误对话框"), QStringLiteral("严重错误"));
	if (ret == QMessageBox::Ok)
		qDebug() << QStringLiteral("严重错误");
	QMessageBox::about(this, QStringLiteral("关于对话框"), QStringLiteral("关于Qt"));
}

//进度对话框
void Widget::on_pushButton_progress_clicked()
{
	QProgressDialog progess(QStringLiteral("文件复制进度"), QStringLiteral("取消"), 0, 50000, this);
	progess.setWindowTitle(QStringLiteral("进度对话框"));
	progess.setWindowModality(Qt::WindowModal); //设为阻塞对话框
	progess.show();

	//模拟文件复制进度
	for (int i = 0; i < 50000; i++)
	{
		progess.setValue(i);
		//        Processes all pending events for the calling thread according to the specified flags until there are no more events to process.
		//        You can call this function occasionally when your program is busy performing a long operation (e.g. copying a file).
		//        In the event that you are running a local loop which calls this function continuously,
		//        without an event loop, the DeferredDelete events will not be processed.
		//        This can affect the behaviour of widgets, e.g. QToolTip, that rely on DeferredDelete events to function properly.
		//        An alternative would be to call sendPostedEvents() from within that local loop.
		//        Calling this function processes events only for the calling thread.
		//        Note: This function is thread-safe.
		//        根据指定的标志 处理调用该函数线程的所有等待事件 直到呢没有事件要处理为止
		//        当你的程序执行一段很耗时的操作时, 偶尔呢可以调用这个函数去解决
		//        如果你不停地在局部循环中调用该函数的话, 如果没有事件,那么不会处理DeferredDelete事件
		//        它会影响widgets, e.g QToolTip的行为
		//        它依靠DeferredDelete事件才能正确地执行
		//        该函数仅针对调用线程有效
		//        另一种方法是从本地循环中调用sendPostedEvents
		//        该函数是线程安全的
		QCoreApplication::processEvents();  //我们说在ui线程中执行耗时操作的话会卡死ui线程
											//为了防止ui线程卡死 让该函数去处理当前线程的所有等待事件
											//等待事件?当前不就是在for中吗 当然是执行for中的相关等待事件啦
		if (progess.wasCanceled())
			break;
	}
	progess.setValue(50000);    //49999 需要再设置一次

	qDebug() << QStringLiteral("文件复制完成!");
}

//错误信息对话框
void Widget::on_pushButton_error_clicked()
{
	QErrorMessage *pError = new QErrorMessage(this);
	if (pError == NULL)
		return;
	pError->setWindowTitle(QStringLiteral("错误信息对话框"));
	pError->showMessage(QStringLiteral("显示一个错误信息"));
}

QWizardPage *Widget::createPage1(QString strTitle)
{
	QWizardPage *pQWizardPage = new QWizardPage(this->m_pQWizard);
	pQWizardPage->setTitle(strTitle);

	return pQWizardPage;
}

QWizardPage *Widget::createPage2(QString strTitle)
{
	QWizardPage *pQWizardPage = new QWizardPage(this->m_pQWizard);
	pQWizardPage->setTitle(strTitle);

	return pQWizardPage;
}

QWizardPage *Widget::createPage3(QString strTitle)
{
	QWizardPage *pQWizardPage = new QWizardPage(this->m_pQWizard);
	pQWizardPage->setTitle(strTitle);

	return pQWizardPage;
}

//向导对话框
void Widget::on_pushButton_guide_clicked()
{
	this->m_pQWizard->addPage(this->createPage1(QStringLiteral("介绍")));
	this->m_pQWizard->addPage(this->createPage2(QStringLiteral("用户选择信息")));
	this->m_pQWizard->addPage(this->createPage3(QStringLiteral("结束")));

	this->m_pQWizard->exec();  //接收处理事件
}


不要排斥Qt慢慢你会发现确实比MFC香 “有了新欢不忘就爱!”@_@别打我...
Qt基础书籍:https://pan.baidu.com/s/1mXb0hcGt0CjigYqFICPubA 密码:js5f

免费评分

参与人数 1吾爱币 +1 热心值 +1 收起 理由
zxbfy021105 + 1 + 1 热心回复!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

莫名堂 发表于 2020-8-20 08:21
谢谢分享
lizf2019 发表于 2020-8-20 08:33
头像被屏蔽
tlf 发表于 2020-8-20 08:39
qcz00622 发表于 2020-8-20 08:42
谢谢分享,QT源文件安装貌似不容易。。。
sergin 发表于 2020-8-20 08:44
谢谢分享
涛之雨 发表于 2020-8-20 08:47
qt主要安装控件有点麻烦。。。
我装不了
一般c用vs2013,装完连接的时候出错。。。
zdzwy 发表于 2020-8-20 08:52
谢谢分享,感觉好复杂
初音ミク 发表于 2020-8-20 09:11
涛之雨 发表于 2020-8-20 08:47
qt主要安装控件有点麻烦。。。
我装不了
一般c用vs2013,装完连接的时候出错。。。

最新版安装老慢了  之前我挂了一夜还没下完  
JuncoJet 发表于 2020-8-20 09:15
楼主这QT高级玩法把,大多数人都拖放控件可视化编程的好么
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止回复与主题无关非技术内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-6-5 14:34

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表