■ QToolBox
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
this->setGeometry(0, 0, 800, 480);
toolBox = new QToolBox(this);
toolBox->setGeometry(300, 50, 200, 250);
toolBox->setStyleSheet("QToolBox {background-color:rgba(0, 0, 0, 30%);}");
for(int i = 0; i < 2; i++){
vBoxLayout[i] = new QVBoxLayout();
groupBox[i] = new QGroupBox(this);
}
QList <QString>strList;
strList<<"李白"<<"王照君"<<"李元芳"<<"程咬金"<<"钟馗"<<"上官婉儿";
QList <QString>iconsList;
iconsList<<":/icons/libai"<<":/icons/wangzhaojun"
<<":/icons/liyuanfang"<<":/icons/chengyaojin"
<<":/icons/zhongkui"<<":/icons/shangguanwaner";
for(int i = 0; i < 6; i++){
toolButton[i] = new QToolButton();
toolButton[i]->setIcon(QIcon(iconsList[i]));
toolButton[i]->setText(strList[i]);
toolButton[i]->setFixedSize(150, 40);
toolButton[i]->setToolButtonStyle(
Qt::ToolButtonTextBesideIcon
);
if( i < 3 ) {
vBoxLayout[0]->addWidget(toolButton[i]);
vBoxLayout[0]->addStretch(1);
} else {
vBoxLayout[1]->addWidget(toolButton[i]);
vBoxLayout[1]->addStretch(1);
}
}
groupBox[0]->setLayout(vBoxLayout[0]);
groupBox[1]->setLayout(vBoxLayout[1]);
toolBox->addItem(groupBox[0],"我的好友");
toolBox->addItem(groupBox[1],"黑名单");
}
MainWindow::~MainWindow()
{
}
■ QGroupBox
this->setGeometry(0, 0, 800, 480);
groupBox = new QGroupBox(tr("QGroupBox示例"), this);
groupBox->setGeometry(300, 100, 300, 200);
vBoxLayout = new QVBoxLayout();
QList <QString>list;
list<<"选项一"<<"选项二"<<"选项三";
for(int i = 0; i < 3; i++){
radioButton[i] = new QRadioButton();
radioButton[i]->setText(list[i]);
vBoxLayout->addWidget(radioButton[i]);
}
vBoxLayout->addStretch(1);
groupBox->setLayout(vBoxLayout);
QGroupBox {
border-width:1px;
border-style:solid;
border-color:lightGray;
margin-top: 0.5ex;
}
QGroupBox::title {
subcontrol-origin: margin;
subcontrol-position: top left;
left:25px;
margin-left: 0px;
padding:0 1px;
}
QGroupBox::title {
subcontrol-origin: margin;
position: relative;
left: 12px;
}
ui->groupBox->setStyleSheet("background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #E0E0E0, stop: 1 #FFFFFF);"
" border-radius: 5px; margin-top: 1ex; ");
■ QCheckBox
示例一: 三态按钮 ,
QCheckBox{
spacing: 5px;
color: white;
}
QCheckBox::indicator {
width: 50px;
height: 50px;
}
QCheckBox::indicator:enabled:unchecked {
image: url(:/images/unchecked.png);
}
QCheckBox::indicator:enabled:checked {
image: url(:/images/checked.png);
}
QCheckBox::indicator:enabled:indeterminate {
image: url(:/images/indeterminate.png);
}
checkBox = new QCheckBox(this);
checkBox->setGeometry(350, 200, 250, 50);
checkBox->setCheckState(Qt::Checked);
checkBox->setText("初始化为Checked状态");
checkBox->setTristate();
connect(checkBox, SIGNAL(stateChanged(int)), this, SLOT(checkBoxStateChanged(int)));
void MainWindow::checkBoxStateChanged(int state)
{
switch (state) {
case Qt::Checked:
checkBox->setText("Checked状态");
break;
case Qt::Unchecked:
checkBox->setText("Unchecked状态");
break;
case Qt::PartiallyChecked:
checkBox->setText("PartiallyChecked状态");
break;
default:
break;
}
}
■ QComboBox
示例一:
comboBox = new QComboBox(this);
comboBox->setGeometry(300, 200, 150, 30);
comboBox->addItem("广东(默认)");
comboBox->addItem("湖南");
comboBox->addItem("四川");
connect(comboBox, SIGNAL(currentIndexChanged(int)), this,
SLOT(comboBoxIndexChanged(int)));
void MainWindow::comboBoxIndexChanged(int index)
{
qDebug()<<"您选择的省份是"<< comboBox->itemText(index)<<endl;
}
■ QFontComboBox
fontComboBox = new QFontComboBox(this);
label = new QLabel(this);
fontComboBox->setGeometry(280, 200, 200, 30);
label->setGeometry(280, 250, 300, 50);
connect(fontComboBox, SIGNAL(currentFontChanged(QFont)), this,
SLOT(fontComboBoxFontChanged(QFont)));
MainWindow::~MainWindow()
{
}
void MainWindow::fontComboBoxFontChanged(QFont font)
{
label->setFont(font);
QString str = "用此标签显示字体效果\n设置的字体为:" +
fontComboBox->itemText(fontComboBox->currentIndex());
label->setText(str);
}
■ QSpinBox
spinBox = new QSpinBox(this);
spinBox->setGeometry(350, 200, 150, 30);
spinBox->setRange(0, 100);
spinBox->setSingleStep(10);
spinBox->setValue(100);
spinBox->setSuffix("%不透明度");
connect(spinBox,SIGNAL(valueChanged(int)), this, SLOT(spinBoxValueChanged(int)));
void MainWindow::spinBoxValueChanged(int opacity)
{
double dobleopacity = (double)opacity / 100;
this->setWindowOpacity(dobleopacity);
}
■ QDoubleSpinBox
doubleSpinBox = new QDoubleSpinBox(this);
doubleSpinBox->setGeometry((this->width() - 200) / 2,
(this->height() - 30) / 2,
200, 30);
doubleSpinBox->setPrefix("窗口大小");
doubleSpinBox->setSuffix("%");
doubleSpinBox->setRange(50.00, 100.00);
doubleSpinBox->setValue(100.00);
doubleSpinBox->setSingleStep(0.1);
connect(doubleSpinBox, SIGNAL(valueChanged(double)),
SLOT(doubleSpinBoxValueChanged(double)));
void MainWindow::doubleSpinBoxValueChanged(double value)
{
int w = 800 * value / 100;
int h = 480 * value / 100;
this->setGeometry(0, 0, w, h);
doubleSpinBox->setGeometry((this->width() - 200) / 2,
(this->height() - 30) / 2,
200, 30);
}
■
■
■