打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
Qt之对话框设计
  1. #include "maindlg.h"  
  2. #include <QPalette>  
  3. #include <QHBoxLayout>  
  4. #include <QVBoxLayout>  
  5. #include <QGridLayout>  
  6. #include <QStringList>  
  7. #include <QColor>  
  8. #include <QPixmap>  
  9. #include <QSpinBox>  
  10. #include <QTextEdit>  
  11. #include <QLineEdit>  
  12. #include <QPushButton>  
  13. #include <QLabel>  
  14.   
  15. MainDlg::MainDlg(QWidget *parent, Qt::WFlags flags)  
  16.     : QDialog(parent, flags)  
  17. {  
  18.     createCtrlFrame();  
  19.     createContentFrame();  
  20.   
  21.     QHBoxLayout *mainLayout = new QHBoxLayout(this);  
  22.     mainLayout->addWidget(ctrlFrame);  
  23.     mainLayout->addWidget(contentFrame);  
  24.     mainLayout->setMargin(10);  
  25.     mainLayout->setSpacing(5);  
  26.     mainLayout->setSizeConstraint(QLayout::SetFixedSize);  
  27. }  
  28.   
  29. MainDlg::~MainDlg()  
  30. {  
  31.   
  32. }  
  33.   
  34. void MainDlg::fillColorList(QComboBox *cbb)  
  35. {  
  36.     QStringList colorNameList = QColor::colorNames();  
  37.   
  38.     QString colorName;  
  39.     foreach(colorName,colorNameList)  
  40.     {  
  41.         QPixmap pix_color(70,20);  
  42.         pix_color.fill(QColor(colorName));  
  43.   
  44.         cbb->addItem(QIcon(pix_color),NULL);  
  45.         cbb->setIconSize(QSize(70,20));  
  46.         cbb->setSizeAdjustPolicy(QComboBox::AdjustToContents);   //设置下拉列表的尺寸符合内容的大小  
  47.     }  
  48. }  
  49.   
  50. void MainDlg::createCtrlFrame()  
  51. {  
  52.     ctrlFrame = new QFrame;  
  53.   
  54.     QLabel *labWindow = new QLabel(tr("QPalette::Window:"));  
  55.     QLabel *labWindowText = new QLabel(tr("QPalette::WindowText:"));  
  56.     QLabel *labButton = new QLabel(tr("QPalette::Button:"));  
  57.     QLabel *labButtonText = new QLabel(tr("QPalette::ButtonText:"));  
  58.     QLabel *labBase = new QLabel(tr("QPalette::Base:"));  
  59.   
  60.     cbbWindow = new QComboBox;  
  61.     fillColorList(cbbWindow);  
  62.     connect(cbbWindow,SIGNAL(activated(int)),this,SLOT(sl_window()));  
  63.     cbbWindowText = new QComboBox;  
  64.     fillColorList(cbbWindowText);  
  65.     connect(cbbWindowText,SIGNAL(activated(int)),this,SLOT(sl_windowText()));  
  66.     cbbButton = new QComboBox;  
  67.     fillColorList(cbbButton);  
  68.     connect(cbbButton,SIGNAL(activated(int)),this,SLOT(sl_button()));  
  69.     cbbButtonText = new QComboBox;  
  70.     fillColorList(cbbButtonText);  
  71.     connect(cbbButtonText,SIGNAL(activated(int)),this,SLOT(sl_buttonText()));  
  72.     cbbBase = new QComboBox;  
  73.     fillColorList(cbbBase);  
  74.     connect(cbbBase,SIGNAL(activated(int)),this,SLOT(sl_base()));  
  75.       
  76.     int col_lab = 0;  
  77.     int col_cbb = 1;  
  78.     QGridLayout *ctrlLayout = new QGridLayout(ctrlFrame);  
  79.     ctrlLayout->addWidget(labWindow,0,col_lab);  
  80.     ctrlLayout->addWidget(labWindowText,1,col_lab);  
  81.     ctrlLayout->addWidget(labButton,2,col_lab);  
  82.     ctrlLayout->addWidget(labButtonText,3,col_lab);  
  83.     ctrlLayout->addWidget(labBase,4,col_lab);  
  84.     ctrlLayout->addWidget(cbbWindow,0,col_cbb);  
  85.     ctrlLayout->addWidget(cbbWindowText,1,col_cbb);  
  86.     ctrlLayout->addWidget(cbbButton,2,col_cbb);  
  87.     ctrlLayout->addWidget(cbbButtonText,3,col_cbb);  
  88.     ctrlLayout->addWidget(cbbBase,4,col_cbb);  
  89.     ctrlLayout->setMargin(5);  
  90.     ctrlLayout->setSpacing(5);  
  91. }  
  92.   
  93. void MainDlg::createContentFrame()  
  94. {  
  95.     contentFrame = new QFrame;  
  96.       
  97.     QLabel *labValue = new QLabel(tr("Please select one of the values:"));  
  98.     QSpinBox *spbValue = new QSpinBox;  
  99.     QHBoxLayout *valueLayout = new QHBoxLayout;  
  100.     valueLayout->addWidget(labValue);  
  101.     valueLayout->addWidget(spbValue);  
  102.     valueLayout->setSpacing(5);  
  103.   
  104.     QLabel *labString = new QLabel(tr("Please input a string"));  
  105.     QLineEdit *edtString = new QLineEdit;  
  106.     QHBoxLayout *stringLayout = new QHBoxLayout;  
  107.     stringLayout->addWidget(labString);  
  108.     stringLayout->addWidget(edtString);  
  109.     stringLayout->setSpacing(5);  
  110.   
  111.     QTextEdit *edtHelloQt = new QTextEdit(tr("Hello Qt!"));  
  112.       
  113.     QPushButton *btnOk = new QPushButton(tr("OK"));  
  114.     QPushButton *btnCancel =new QPushButton(tr("Cancel"));  
  115.     QHBoxLayout *buttonLayout = new QHBoxLayout;  
  116.     buttonLayout->addStretch(1);  
  117.     buttonLayout->addWidget(btnOk);  
  118.     buttonLayout->addWidget(btnCancel);  
  119.     buttonLayout->setSpacing(5);  
  120.   
  121.     QVBoxLayout *contentLayout = new QVBoxLayout(contentFrame);  
  122.     contentLayout->addLayout(valueLayout);  
  123.     contentLayout->addLayout(stringLayout);  
  124.     contentLayout->addWidget(edtHelloQt);  
  125.     contentLayout->addLayout(buttonLayout);  
  126.     contentLayout->setMargin(5);  
  127.     contentLayout->setSpacing(5);  
  128.   
  129.     btnOk->setAutoFillBackground(true);  
  130.     btnCancel->setAutoFillBackground(true);  
  131.     contentFrame->setAutoFillBackground(true);  
  132. }  
  133.   
  134. void MainDlg::sl_window()  
  135. {  
  136.     QStringList colorList = QColor::colorNames();  
  137.     QColor color = QColor(colorList[cbbWindow->currentIndex()]);  
  138.     QPalette p = contentFrame->palette();  
  139.     p.setColor(QPalette::Window,color);  
  140.     contentFrame->setPalette(p);  
  141. }  
  142.   
  143. void MainDlg::sl_windowText()  
  144. {  
  145.     QStringList colorList = QColor::colorNames();  
  146.     QColor color = QColor(colorList[cbbWindowText->currentIndex()]);  
  147.     QPalette p = contentFrame->palette();  
  148.     p.setColor(QPalette::WindowText,color);  
  149.     contentFrame->setPalette(p);  
  150. }  
  151.   
  152. void MainDlg::sl_button()  
  153. {  
  154.     QStringList colorNameList = QColor::colorNames();  
  155.     QColor m_color = QColor(colorNameList[cbbButton->currentIndex()]);  
  156.     QPalette p = contentFrame->palette();  
  157.     p.setColor(QPalette::Button,m_color);  
  158.     contentFrame->setPalette(p);  
  159. }  
  160.   
  161. void MainDlg::sl_buttonText()  
  162. {  
  163.     QStringList colorNameList = QColor::colorNames();  
  164.     QColor m_color = QColor(colorNameList[cbbButtonText->currentIndex()]);  
  165.     QPalette p = contentFrame->palette();  
  166.     p.setColor(QPalette::ButtonText,m_color);  
  167.     contentFrame->setPalette(p);  
  168. }  
  169.   
  170.   
  171. void MainDlg::sl_base()  
  172. {  
  173.     QStringList colorNameList = QColor::colorNames();  
  174.     QColor m_color = QColor(colorNameList[cbbBase->currentIndex()]);  
  175.     QPalette p = contentFrame->palette();  
  176.     p.setColor(QPalette::Base,m_color);  
  177.     contentFrame->setPalette(p);  
  178. }  
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
PyQt5执行耗时操作导致主页面暂时性卡死问题
PyQt5之布局管理
Qfileinfo
Python 开发桌面程序,PyQt 实现计数器
Qt-----进度条QProgressBar
【第八节】PyQt5控件(II)
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服