26.QT颜色与布局
Posted 喵小喵~
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了26.QT颜色与布局相关的知识,希望对你有一定的参考价值。
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颜色与布局的主要内容,如果未能解决你的问题,请参考以下文章