QMessageBox 窗口大小更改问题(thinkvd开发日志)

      QMessageBox的功能很通用、实用及方便,相信用过Qt的开发者都用过它,但它有一个让人不友好的地方,就是不能设置窗口的大小,尤其对自定义界面的窗口风格时,不得不考虑这个不便。这个问题在自己参与开发thinkvd时就意识到,由于当时自己对Qt的认识有限,几次偿试解决都没有结果,当时主要原因在于它引用了几个类的私有成员(如QLabel的)。

 

     现在把实现的思路大概说一下:
  1。重新实现QMessageBox->ImMessageBox(直接COPY QMessageBox),改名,再带上qdialog_p.h
  2。去掉ImMessageBoxPrivate中的QLabel中的私有成员调用.(它调整宽度用的,暂时不用它)
      3. ImMessageBoxPrivate增加成员变量: m_size记录自定义的窗口大小(赋值的代码自己写吧就几行)
      4.  void ImMessageBoxPrivate::updateSize()中:

 q->layout()->activate();
    int height = (q->layout()->hasHeightForWidth())
                     ? q->layout()->totalHeightForWidth(width)
                     : q->layout()->totalMinimumSize().height();
- q->setFixedSize(width, height);
+ f (m_size.isValid())
+   q->setFixedSize(m_size);
+  else
+   q->setFixedSize(width, height);
    QCoreApplication::removePostedEvents(q, QEvent::LayoutRequest);


调用样例:
方法1:  
 ImMessageBox *msgBox = new ImMessageBox(this);
 msgBox->resize(300, 350);
 msgBox->exec();
方法2:
 ImMessageBox::warning(this, "title", "dddddddddddddddd", QSize(300, 300) );
( 这里只用了一个warning说明,还有其它几个需要作相应的改动);

 

方法3
 ImMessageBox *msgBox = new ImMessageBox(ImMessageBox::Warning, "title", "dddddddddd", ImMessageBox::Ok, this, Qt::Dialog);
 msgBox->setFixedSize(300, 150);
 msgBox->exec();

你可能感兴趣的:(C++,C语言)