1. 程式人生 > >26.QT顏色與布局

26.QT顏色與布局

下拉 contents vbox nal style qpi size dialog endif

技術分享圖片

dialog.h

 1 #ifndef PALETTE_H
 2 #define PALETTE_H
 3 
 4 #include <QDialog>
 5 #include <QComboBox>
 6 #include <QLabel>
 7 #include <QTextEdit>
 8 #include <QPushButton>
 9 #include <QLineEdit>
10 
11 class Palette : public QDialog
12 {
13     Q_OBJECT
14 
15
public: 16 Palette(QWidget *parent = 0); 17 ~Palette(); 18 19 //創建左邊布局 20 void createCtrlFrame(); 21 //創建右邊布局 22 void createContentFrame(); 23 //給下拉框填充顏色 24 void fillColorList(QComboBox *); 25 26 private slots: 27 void showWindow(); 28 void showWindowText(); 29 void
showButton(); 30 void showButtonText(); 31 void showBase(); 32 33 private: 34 QFrame *ctrlFrame; 35 QLabel *windowLabel; 36 QComboBox *windowComboBox; 37 QLabel *windowTextLabel; 38 QComboBox *windowTextComboBox; 39 QLabel *buttonLabel; 40 QComboBox *buttonComboBox;
41 QLabel *buttonTextLabel; 42 QComboBox *buttonTextComboBox; 43 QLabel *baseLabel; 44 QComboBox *baseComboBox; 45 QFrame *contentFrame; 46 QLabel *label1; 47 QComboBox *comboBox1; 48 QLabel *label2; 49 QLineEdit *lineEdit2; 50 QTextEdit *textEdit; 51 QPushButton *okBtn; 52 QPushButton *cancelBtn; 53 }; 54 55 #endif // PALETTE_H

dialog.cpp

  1 #include "dialog.h"
  2 #include <QHBoxLayout>
  3 #include <QGridLayout>
  4 #include <QPalette>
  5 #include <QBoxLayout>
  6 
  7 Palette::Palette(QWidget *parent)
  8     : QDialog(parent)
  9 {
 10     createCtrlFrame();
 11     createContentFrame();
 12     QHBoxLayout *mainLayout = new QHBoxLayout(this);
 13     mainLayout->addWidget(ctrlFrame);
 14     mainLayout->addWidget(contentFrame);
 15 }
 16 
 17 //創建左邊布局
 18 void Palette::createCtrlFrame()
 19 {
 20     //創建框架
 21     ctrlFrame = new QFrame;
 22 
 23     //窗口背景色
 24     windowLabel = new QLabel(tr("控件背景色: "));
 25     windowComboBox = new QComboBox;
 26     fillColorList(windowComboBox);
 27     //控件與時間綁定
 28     connect(windowComboBox,SIGNAL(activated(int)),this,SLOT(showWindow()));
 29 
 30      //窗口前景色
 31     windowTextLabel = new QLabel(tr("窗口字體顏色: "));
 32     windowTextComboBox = new QComboBox;
 33     fillColorList(windowTextComboBox);
 34     connect(windowTextComboBox,SIGNAL(activated(int)),this,SLOT(showWindowText()));
 35 
 36     //窗口按鈕的顏色
 37     buttonLabel = new QLabel(tr("窗口按鈕的顏色: "));
 38     buttonComboBox = new QComboBox;
 39     fillColorList(buttonComboBox);
 40     connect(buttonComboBox,SIGNAL(activated(int)),this,SLOT(showButton()));
 41 
 42     //窗口按鈕上面文本的顏色
 43     buttonTextLabel = new QLabel(tr("窗口按鈕上面文本的顏色: "));
 44     buttonTextComboBox = new QComboBox;
 45     fillColorList(buttonTextComboBox);
 46     connect(buttonTextComboBox,SIGNAL(activated(int)),this,SLOT(showButtonText()));
 47 
 48     //編輯框背景色
 49     baseLabel = new QLabel(tr("編輯框背景色: "));
 50     baseComboBox = new QComboBox;
 51     fillColorList(baseComboBox);
 52     connect(baseComboBox,SIGNAL(activated(int)),this,SLOT(showBase()));
 53 
 54     //創建網格布局,框架是ctrFrame
 55     QGridLayout *mainLayout = new QGridLayout(ctrlFrame);
 56     //設置間距
 57     mainLayout->setSpacing(20);
 58     mainLayout->addWidget(windowLabel,0,0);
 59     mainLayout->addWidget(windowComboBox,0,1);
 60 
 61     mainLayout->addWidget(windowTextLabel,1,0);
 62     mainLayout->addWidget(windowTextComboBox,1,1);
 63 
 64     mainLayout->addWidget(buttonLabel,2,0);
 65     mainLayout->addWidget(buttonComboBox,2,1);
 66 
 67     mainLayout->addWidget(buttonTextLabel,3,0);
 68     mainLayout->addWidget(buttonTextComboBox,3,1);
 69 
 70     mainLayout->addWidget(baseLabel,4,0);
 71     mainLayout->addWidget(baseComboBox,4,1);
 72 
 73 }
 74 
 75 //創建右邊布局
 76 void Palette::createContentFrame()
 77 {
 78     contentFrame = new QFrame;
 79     label1 = new QLabel(tr("請選擇一個值:"));
 80     comboBox1 = new QComboBox;
 81 
 82     label2 = new QLabel(tr("請輸入字符串: "));
 83     lineEdit2 = new QLineEdit;
 84 
 85     textEdit = new QTextEdit;
 86 
 87     //創建網格布局
 88     QGridLayout *topLayout = new QGridLayout;
 89     topLayout->addWidget(label1,0,0);
 90     topLayout->addWidget(comboBox1,0,1);
 91     topLayout->addWidget(label2,1,0);
 92     topLayout->addWidget(lineEdit2,1,1);
 93     topLayout->addWidget(textEdit,2,0,1,2);
 94 
 95     okBtn = new QPushButton(tr("確認"));
 96     cancelBtn = new QPushButton(tr("取消"));
 97 
 98     //創建水平布局
 99     QHBoxLayout *bottomLayout = new QHBoxLayout;
100     //設置伸縮性
101     bottomLayout->addStretch(1);
102     bottomLayout->addWidget(okBtn);
103     bottomLayout->addWidget(cancelBtn);
104 
105     //創建垂直布局,把兩個布局加入,框架是contentFrame
106     QVBoxLayout *mainlayout = new QVBoxLayout(contentFrame);
107     mainlayout->addLayout(topLayout);
108     mainlayout->addLayout(bottomLayout);
109 
110     //允許自動填充
111     okBtn->setAutoFillBackground(true);
112     cancelBtn->setAutoFillBackground(true);
113     //設置可以填充
114     contentFrame->setAutoFillBackground(true);
115 }
116 
117 //用於控制背景顏色的顯示
118 void Palette::showWindow()
119 {
120     QStringList colorList = QColor::colorNames();
121     QColor color = QColor(colorList[windowComboBox->currentIndex()]);
122 
123 
124     QPalette p = contentFrame->palette();
125     p.setColor(QPalette::Window, color);
126     contentFrame->setPalette(p);
127 
128     contentFrame->update();
129 }
130 
131 //對窗體的前景色進行設置
132 void Palette::showWindowText()
133 {
134     QStringList colorList = QColor::colorNames();
135     QColor color = colorList[windowTextComboBox->currentIndex()];
136 
137     QPalette p = contentFrame->palette();
138     p.setColor(QPalette::WindowText, color);
139     contentFrame->setPalette(p);
140 }
141 
142 //按鈕文字顏色設置
143 void Palette::showButtonText()
144 {
145     QStringList colorList = QColor::colorNames();
146     QColor color = QColor(colorList[buttonTextComboBox->currentIndex()]);
147 
148     QPalette p = contentFrame->palette();
149     p.setColor(QPalette::ButtonText , color);
150     contentFrame->setPalette(p);
151 }
152 
153 //edit背景顏色設置
154 void Palette::showBase()
155 {
156     QStringList colorList = QColor::colorNames();
157     QColor color = QColor(colorList[baseComboBox->currentIndex()]);
158 
159     QPalette p = contentFrame->palette();
160     p.setColor(QPalette::Base , color);
161     contentFrame->setPalette(p);
162 }
163 
164 //控件添加顏色
165 void Palette::fillColorList(QComboBox *comboBox)
166 {
167     QStringList colorList = QColor::colorNames();
168     QString color;
169 
170     foreach (color, colorList)
171     {
172         QPixmap pix(QSize(70,20));
173         pix.fill(QColor(color));
174         comboBox->addItem(QIcon(pix), NULL);
175         comboBox->setIconSize(QSize(70,20));
176         comboBox->setSizeAdjustPolicy(QComboBox::AdjustToContents);
177     }
178 }
179 
180 //按鈕顏色設置
181 void Palette::showButton()
182 {
183     QStringList colorList = QColor::colorNames();
184     QColor color = QColor(colorList[buttonComboBox->currentIndex()]);
185 
186     //contentFrame->setAutoFillBackground(true);
187 
188     QPalette p = contentFrame->palette();
189     p.setColor(QPalette::Button , color);
190     contentFrame->setPalette(p);
191 
192     contentFrame->update();
193 
194 }
195 
196 Palette::~Palette()
197 {
198 
199 }

26.QT顏色與布局