Browse Source

修复弹出的页面不显示内容的问题。
使用非模态弹出窗口时,在 showEvent 中初始化会导致页面空白。
解决办法有三种:
1. 改为模态弹框;
2. 弹框随 parent 的构造函数中创建;
3. 弹框在弹出前创建;

chengxr 1 year ago
parent
commit
43fd5dd3c2

+ 20 - 15
QFD/view/ExpertManageView.cpp

@@ -21,14 +21,9 @@ void ExpertManageView::showEvent(QShowEvent *event)
 {
     qDebug() << __FUNCTION__ << __LINE__;
 
-    if (m_initilized == false) {
-        initialize();
-        initLayout();
-        connectSignalsAndSlots();
-        m_initilized = true;
-    }
-
     QWidget::showEvent(event);
+
+    init();
 }
 
 void ExpertManageView::hideEvent(QHideEvent *event)
@@ -36,6 +31,16 @@ void ExpertManageView::hideEvent(QHideEvent *event)
     QWidget::hideEvent(event);
 }
 
+void ExpertManageView::init()
+{
+    if (m_initilized == false) {
+        initialize();
+        initLayout();
+        connectSignalsAndSlots();
+        m_initilized = true;
+    }
+}
+
 void ExpertManageView::initialize()
 {
     m_vBoxLayout = new QVBoxLayout(this);
@@ -54,7 +59,6 @@ void ExpertManageView::initialize()
     m_deleteButton = new ToolButton(NEWFLICON(FluentIcon, DELETE), this);
     m_deleteButton->setToolTip("删除用户");
 
-    m_expertInfoWidget = new ExpertInfoWidget(this);
     m_expertListWidget = new ExpertListWidget(this);
 }
 
@@ -94,8 +98,12 @@ void ExpertManageView::setListButtonHidden(bool hidden)
     m_deleteButton->setHidden(hidden);
 }
 
-void ExpertManageView::showExpertInfo()
+void ExpertManageView::showExpertInfo(int mode)
 {
+    if (m_expertInfoWidget == nullptr) {
+        m_expertInfoWidget = new ExpertInfoWidget(this);
+    }
+    m_expertInfoWidget->setMode(ExpertInfoWidget::Mode(mode));
     if (m_expertInfoWidget->isVisible() == false) {
         m_expertInfoWidget->clearInputs();
         m_expertInfoWidget->show();
@@ -104,14 +112,12 @@ void ExpertManageView::showExpertInfo()
 
 void ExpertManageView::slotAddExpertClicked()
 {
-    m_expertInfoWidget->setMode(ExpertInfoWidget::Add);
-    showExpertInfo();
+    showExpertInfo(ExpertInfoWidget::Add);
 }
 
 void ExpertManageView::slotDetailClicked()
 {
-    m_expertInfoWidget->setMode(ExpertInfoWidget::Read);
-    showExpertInfo();
+    showExpertInfo(ExpertInfoWidget::Read);
 }
 
 void ExpertManageView::slotDeleteClicked()
@@ -127,6 +133,5 @@ void ExpertManageView::slotListSelectionChanged()
 
 void ExpertManageView::slotListItemDoubleClicked()
 {
-    m_expertInfoWidget->setMode(ExpertInfoWidget::Read);
-    showExpertInfo();
+    showExpertInfo(ExpertInfoWidget::Read);
 }

+ 3 - 1
QFD/view/ExpertManageView.h

@@ -29,13 +29,15 @@ public:
 signals:
 
 private:
+    void init();
+
     void initialize();
     void initLayout();
     void connectSignalsAndSlots();
 
     void setListButtonHidden(bool hidden);
 
-    void showExpertInfo();
+    void showExpertInfo(int mode);
 
 private slots:
     void slotAddExpertClicked();

+ 1 - 0
QFD/view/UserView.h

@@ -27,6 +27,7 @@ private:
 
     void initilaize();
     void initLayout();
+
     void connectSignalsAndSlots();
     void refresh();
 

+ 7 - 3
QFD/widgets/CreateProjWidget.cpp

@@ -9,7 +9,7 @@
 
 #include <QDebug>
 
-CreateProjWidget::CreateProjWidget(QWidget *parent) : QWidget(parent)
+CreateProjWidget::CreateProjWidget(QWidget *parent) : QDialog(parent)
 {
     initWindow();
     initialize();
@@ -42,8 +42,12 @@ void CreateProjWidget::clearInputs()
 void CreateProjWidget::initWindow()
 {
     setWindowTitle("新建工程");
-    setWindowFlags(Qt::Window);
-    setWindowFlag(Qt::WindowMinMaxButtonsHint, false);
+    //    setWindowFlags(Qt::Window);
+    //    setWindowFlag(Qt::WindowMinMaxButtonsHint, false);
+
+    setModal(true);
+    setWindowFlags(Qt::Dialog);
+    setWindowFlag(Qt::WindowContextHelpButtonHint, false);
     resize(400, 250);
 }
 

+ 2 - 2
QFD/widgets/CreateProjWidget.h

@@ -1,7 +1,7 @@
 #ifndef CREATEPROJWIDGET_H
 #define CREATEPROJWIDGET_H
 
-#include <QWidget>
+#include <QDialog>
 
 class QVBoxLayout;
 class QHBoxLayout;
@@ -12,7 +12,7 @@ class CheckBox;
 class PushButton;
 
 /// 创建工程页面
-class CreateProjWidget : public QWidget
+class CreateProjWidget : public QDialog
 {
     Q_OBJECT
 public:

+ 3 - 0
QFD/widgets/ExpertInfoWidget.cpp

@@ -39,6 +39,9 @@ void ExpertInfoWidget::initWindow()
 {
     setWindowFlags(Qt::Window);
     setWindowFlag(Qt::WindowMinMaxButtonsHint, false);
+    //    setModal(true);
+    //    setWindowFlags(Qt::Dialog);
+    //    setWindowFlag(Qt::WindowContextHelpButtonHint, false);
     resize(400, 450);
 }
 

+ 5 - 3
QFD/widgets/ExpertInfoWidget.h

@@ -1,7 +1,7 @@
 #ifndef EXPERTINFOWIDGET_H
 #define EXPERTINFOWIDGET_H
 
-#include <QWidget>
+#include <QDialog>
 
 class LineEdit;
 class PasswordLineEdit;
@@ -20,12 +20,14 @@ class ExpertInfoWidget : public QWidget
     Q_OBJECT
 
 public:
-    enum Mode
+    typedef enum
     {
         Add,     // 新增
         Update,  // 编辑
         Read,    // 查看
-    };
+    } Mode;
+
+    Q_ENUM(Mode)
 
     explicit ExpertInfoWidget(QWidget *parent = nullptr);