【QT阻塞窗口并获取返回值,QT圆角阴影窗口,QT UI加载时间优化】

QT阻塞窗口并获取返回值

.h文件如下

//.h文件

#pragma once

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


class MyMessageBox : public QDialog
{
	Q_OBJECT

public:
	MyMessageBox(QWidget *parent = nullptr);
	~MyMessageBox();
	void setTextMsg(QString str1, QString str2);
private slots:
	void on_pushButton_ok_clicked();
	void on_pushButton_cancel_clicked();
private:
	Ui::MyMessageBoxClass ui;
};

 .cpp文件如下,代码中设置了窗口圆角和阴影,也是QT中比较常用的方法,需要和.ui文件一起配合才行,主要的逻辑是窗口有一个最底层的容器widget透明,作为整个窗口的承载者。然后在他的基础上再加一个widget2,widget2设置圆角和阴影效果就OK了,窗口所有的控件都放到widget2上面。

        窗口阻塞很简单,参考代码照抄就行,具体调用方法如下:



 MyMessageBox Msg;
    Msg.setTextMsg("语音转文字功能超时,", "重新连接才能恢复功能,需要重新连接吗?");
    if (Msg.exec() != QDialog::Accepted) //没有点击确定或者接受按钮
    {
        return;
    }
//.cpp文件

#include "MyMessageBox.h"
#include <QGraphicsDropShadowEffect>


MyMessageBox::MyMessageBox(QWidget *parent)
	: QDialog(parent)
{
	ui.setupUi(this);

	setWindowFlags(Qt::Window | Qt::FramelessWindowHint | Qt::WindowSystemMenuHint | Qt::WindowMinimizeButtonHint | Qt::WindowMaximizeButtonHint);//去掉标题栏
	setAttribute(Qt::WA_TranslucentBackground);//最底层的widget透明
	QGraphicsDropShadowEffect* shadow_effect = new QGraphicsDropShadowEffect(this);
	shadow_effect->setOffset(0, 0);
	shadow_effect->setColor(QColor(68, 68, 68));
	shadow_effect->setBlurRadius(10);
	ui.widget_main->setGraphicsEffect(shadow_effect);

	//设置内层QWidget的边框和背景颜色
	ui.widget_main->setStyleSheet("QWidget#widget_main{border-radius:18px;background-color:#FFFFFF;}");//border:1px solid #FFFFFF;

	connect(ui.closeToolButton, &QToolButton::clicked, this->window(), &QWidget::close);

}

MyMessageBox::~MyMessageBox()
{}

void MyMessageBox::setTextMsg(QString str1, QString str2)
{
	ui.label_msg1->setText(str1);
	ui.label_msg2->setText(str2);
}

void MyMessageBox::on_pushButton_ok_clicked()
{
	done(QDialog::Accepted);
}
void MyMessageBox::on_pushButton_cancel_clicked()
{
	done(QDialog::Rejected);
}

.ui文件  实际上是xml文件

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>MyMessageBoxClass</class>
 <widget class="QDialog" name="MyMessageBoxClass">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>415</width>
    <height>259</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>MyMessageBox</string>
  </property>
  <layout class="QGridLayout" name="gridLayout">
   <item row="0" column="0">
    <widget class="QWidget" name="widget_main" native="true">
     <property name="styleSheet">
      <string notr="true">background-color: rgb(255, 255, 255);</string>
     </property>
     <layout class="QGridLayout" name="gridLayout_2">
      <item row="0" column="0">
       <layout class="QVBoxLayout" name="verticalLayout" stretch="1,3,2">
        <item>
         <layout class="QHBoxLayout" name="horizontalLayout_3">
          <property name="leftMargin">
           <number>0</number>
          </property>
          <property name="rightMargin">
           <number>0</number>
          </property>
          <item>
           <widget class="QWidget" name="widget" native="true">
            <property name="sizePolicy">
             <sizepolicy hsizetype="Preferred" vsizetype="Preferred">
              <horstretch>100</horstretch>
              <verstretch>0</verstretch>
             </sizepolicy>
            </property>
            <property name="minimumSize">
             <size>
              <width>100</width>
              <height>0</height>
             </size>
            </property>
            <property name="styleSheet">
             <string notr="true"/>
            </property>
            <layout class="QGridLayout" name="gridLayout_3">
             <property name="leftMargin">
              <number>10</number>
             </property>
             <property name="rightMargin">
              <number>10</number>
             </property>
             <item row="0" column="0">
              <widget class="QLabel" name="label_title_">
               <property name="styleSheet">
                <string notr="true">font-size:16px;
</string>
               </property>
               <property name="text">
                <string>警告</string>
               </property>
              </widget>
             </item>
             <item row="0" column="1">
              <spacer name="horizontalSpacer">
               <property name="orientation">
                <enum>Qt::Horizontal</enum>
               </property>
               <property name="sizeHint" stdset="0">
                <size>
                 <width>284</width>
                 <height>20</height>
                </size>
               </property>
              </spacer>
             </item>
             <item row="0" column="2">
              <widget class="QToolButton" name="closeToolButton">
               <property name="text">
                <string/>
               </property>
              </widget>
             </item>
            </layout>
           </widget>
          </item>
         </layout>
        </item>
        <item>
         <layout class="QVBoxLayout" name="verticalLayout_3">
          <property name="topMargin">
           <number>0</number>
          </property>
          <property name="bottomMargin">
           <number>0</number>
          </property>
          <item>
           <widget class="QWidget" name="widget_2" native="true">
            <property name="styleSheet">
             <string notr="true"/>
            </property>
            <layout class="QGridLayout" name="gridLayout_4">
             <property name="topMargin">
              <number>20</number>
             </property>
             <property name="bottomMargin">
              <number>20</number>
             </property>
             <item row="0" column="0">
              <widget class="QLabel" name="label_msg1">
               <property name="styleSheet">
                <string notr="true">font-size:16px;</string>
               </property>
               <property name="text">
                <string/>
               </property>
               <property name="alignment">
                <set>Qt::AlignCenter</set>
               </property>
              </widget>
             </item>
             <item row="1" column="0">
              <widget class="QLabel" name="label_msg2">
               <property name="styleSheet">
                <string notr="true">font-size:16px;</string>
               </property>
               <property name="text">
                <string/>
               </property>
               <property name="alignment">
                <set>Qt::AlignCenter</set>
               </property>
              </widget>
             </item>
            </layout>
           </widget>
          </item>
         </layout>
        </item>
        <item>
         <layout class="QHBoxLayout" name="horizontalLayout_2">
          <property name="spacing">
           <number>10</number>
          </property>
          <property name="rightMargin">
           <number>10</number>
          </property>
          <property name="bottomMargin">
           <number>10</number>
          </property>
          <item>
           <spacer name="horizontalSpacer_2">
            <property name="orientation">
             <enum>Qt::Horizontal</enum>
            </property>
            <property name="sizeHint" stdset="0">
             <size>
              <width>40</width>
              <height>20</height>
             </size>
            </property>
           </spacer>
          </item>
          <item>
           <widget class="QPushButton" name="pushButton_cancel">
            <property name="sizePolicy">
             <sizepolicy hsizetype="Fixed" vsizetype="Fixed">
              <horstretch>100</horstretch>
              <verstretch>32</verstretch>
             </sizepolicy>
            </property>
            <property name="minimumSize">
             <size>
              <width>100</width>
              <height>32</height>
             </size>
            </property>
            <property name="styleSheet">
             <string notr="true">border-radius: 8px;
border: 1px solid #E44C5E;
color:#E44C5E;
font-size:16px;</string>
            </property>
            <property name="text">
             <string>取消</string>
            </property>
           </widget>
          </item>
          <item>
           <widget class="QPushButton" name="pushButton_ok">
            <property name="sizePolicy">
             <sizepolicy hsizetype="Fixed" vsizetype="Fixed">
              <horstretch>100</horstretch>
              <verstretch>32</verstretch>
             </sizepolicy>
            </property>
            <property name="minimumSize">
             <size>
              <width>100</width>
              <height>32</height>
             </size>
            </property>
            <property name="styleSheet">
             <string notr="true">background: #ED6D1F;
border-radius: 8px;
color: rgb(255, 255, 255);
font-size:16px;</string>
            </property>
            <property name="text">
             <string>确定</string>
            </property>
           </widget>
          </item>
         </layout>
        </item>
       </layout>
      </item>
     </layout>
    </widget>
   </item>
  </layout>
 </widget>
 <layoutdefault spacing="6" margin="11"/>
 <resources/>
 <connections/>
</ui>

.ui文件不是重点,展示.ui文件主要是为了表达圆角窗口带阴影怎么制作

第二个话题:QTUI启动慢的问题,我总结的主要原因有这些:

1.在UI类的构造函数或者初始化函数调用了费时的操作,比如说第三方SDK的初始化啥的,这个自行去判断,最好的习惯是第三方SDK都不要直接在UI线程操作

2.UI加载了外部的一些资源,比如说大的图片,或者字体,有些特殊格式的字体加载很费时间,

图片可以延迟加载。字体选择比较容易加载的字体格式,然后尽量每种字体只读取一次,然后全局使用。

3.实在是UI太复杂还可以采用QTimer::singleShot()去加载暂时不用显示的UI,主窗口的构造函数只加载首页需要展示的内容,其他的放到singleShot里面去加载。当然这个也有他的缺点。自己把握

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值