1.x
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | #ifndef WIDGET_H #define WIDGET_H #include <QWidget> #include <QtWidgets> QT_BEGIN_NAMESPACE namespace Ui {<!-- --> class Widget; } QT_END_NAMESPACE class Widget : public QWidget {<!-- --> Q_OBJECT public: Widget(QWidget *parent = nullptr); ~Widget(); private: QCheckBox *checkbox; QPushButton *button; QLineEdit *lineEdit; Ui::Widget *ui; }; #endif // WIDGET_H |
1.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | #include "widget.h" #include "ui_widget.h" #include <QtWidgets> Widget::Widget(QWidget *parent) : QWidget(parent) , ui(new Ui::Widget) {<!-- --> ui->setupUi(this); setFixedSize(400,400); checkbox = new QCheckBox(tr("TEST-A")); button = new QPushButton(tr("TEST-B")); lineEdit = new QLineEdit(); QVBoxLayout *vbox=new QVBoxLayout(this); vbox->addWidget(checkbox); vbox->addWidget(button); vbox->addWidget(lineEdit); } Widget::~Widget() {<!-- --> delete ui; } |
main.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | #include "widget.h" #include <QApplication> int main(int argc, char *argv[]) {<!-- --> QApplication a(argc, argv); a.setStyleSheet("QCheckBox{ background-color: yellow;}" "QCheckBox::indicator,QRadioButton::indicator{ width:30px; height:30px;}" "QCheckBox,QRadioButton{spacing :10px;}" //悬停之后的颜色 "QCheckBox:hover{color:blue;}" //选中之后再悬停之后的颜色 "QCheckBox:check:hover{color:red;}" "QCheckBox:check,QCheckBox:hover{color:red;}" "QPushButton{ background-color: red; color: yellow;}" "QLineEdit{color:rgb(127,0,63);" "background-color:rgb(255,255,241);" "selection-color:white;" "selection-background-color:rgb(191,31,127);" "border;2px groove gray;" "border-radius:10px; " "padding:2px 4px;}" ); Widget w; w.show(); return a.exec(); } |
result:

2.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | #ifndef DIALOG_H #define DIALOG_H #include <QDialog> #include <QtWidgets> QT_BEGIN_NAMESPACE namespace Ui {<!-- --> class Dialog; } QT_END_NAMESPACE class Dialog : public QDialog {<!-- --> Q_OBJECT public: Dialog(QWidget *parent = nullptr); ~Dialog(); private: QLabel *label1; QLabel *label2; QGridLayout *layout1; QLineEdit *lineEdit; QPushButton *button; Ui::Dialog *ui; }; #endif // DIALOG_H |
2.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | #include "dialog.h" #include "ui_dialog.h" #include <QtWidgets> Dialog::Dialog(QWidget *parent) : QDialog(parent) , ui(new Ui::Dialog) {<!-- --> ui->setupUi(this); layout1=new QGridLayout(this); label1 =new QLabel(this); label2 =new QLabel(this); //label1->setAlignment(Qt::AlignCenter); label1->setText("666"); label2->setText("Qt,Qt,Qt!"); lineEdit =new QLineEdit(this); //单独设置样式属性 //lineEdit->setStyleSheet("color:rgb(0,0,255);"); button = new QPushButton("this"); layout1->addWidget(label1,0,0); layout1->addWidget(label2,0,1); layout1->addWidget(lineEdit,1,0); layout1->addWidget(button,1,1); } Dialog::~Dialog() {<!-- --> delete ui; } |
mian.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | #include "dialog.h" #include <QApplication> int main(int argc, char *argv[]) {<!-- --> QApplication a(argc, argv); Dialog w; w.setStyleSheet( "QDialog{background-image:url(C:/Users/xxx/Pictures/RGB.jpg);}" "QLabel{font:20pt;color:rgb(0,0,127);border:1px solid red;}" //字体和背景颜色 "QLineEdit{color:rgb(255,0,0);" "background-color:rgb(255,255,127);" //选中后字体颜色和字体背景色 "selection-color:white;" "selection-background-color:rgb(0,0,255);" "border:5px groove gray;" "border-radius:10px; " "padding:2px 4px;}" "QPushButton{color :white;font:bold 10pt;border-image:url(C:/Users/xxx/Pictures/Qt0.jpg) 16;border-width:16px;" "padding:-16px 0px;min-height:32px;min-width:60px;}" "QPushButton:pressed{color:lightgray;border-image:url(C:/Users/xxx/Pictures/school.jpg) 16;padding-top:-15px;padding-bottom:-17px;}" ); w.show(); return a.exec(); } |
result:
