首页 > 代码库 > 2.QLabel,QPushButton,QLineEdit,QComboBox,QCheckBox,QRadioButton,QTextEdit,QTextBrowser,QGroupBox,QSl
2.QLabel,QPushButton,QLineEdit,QComboBox,QCheckBox,QRadioButton,QTextEdit,QTextBrowser,QGroupBox,QSl
2 添加新文件(选择C++Class)
MyWidget.h |
#ifndef MYWIDGET_H #define MYWIDGET_H #include <QWidget> #include <QTextEdit> class MyWidget : public QWidget { Q_OBJECT public: explicit MyWidget(QWidget *parent = 0); QTextEdit* _edit; signals: public slots: void slotComboBoxIndexChanged(const QString&); }; #endif // MYWIDGET_H |
05Controls.pro文件的内容 |
HEADERS += \ MyWidget.h SOURCES += \ MyWidget.cpp QT += widgets gui CONFIG += C++11 |
MyWidget.cpp |
#include"MyWidget.h" #include<QApplication>
#include<QLabel> //displystatictext,orimage #include<QPushButton> #include<QLineEdit> //singleline
#include<QComboBox> //下拉选 #include<QCheckBox> //复选框 #include<QRadioButton> //单选按钮 #include<QTextEdit> //multilines,richtext #include<QTextBrowser> //readonlytextedit #include<QGroupBox> #include<QSlider> //模拟方式显示数值 #include<QSpinBox> //数字 #include<QDateEdit> #include<QTimeEdit> #include<QDateTimeEdit> #include<QTabWidget> //不介绍,不算控件 #include<QCompleter> #include<QHBoxLayout> //布局控件 #include<QDebug> //这里用于测试用 #include<QPixmap> #include<QVBoxLayout> #include<QLCDNumber>
MyWidget::MyWidget(QWidget*parent): QWidget(parent) { QVBoxLayout*lay=newQVBoxLayout(this); QLabel*label; QComboBox*combo; QPushButton*button; QRadioButton*radio; QTextEdit*edit; QGroupBox*group;
//说明可以通过编写样式的方式修改GUI中字体的样式 lay->addWidget(label=newQLabel("<ahref=http://www.mamicode.com/www.baidu.com>百度</a>")); //通过下面一句在lable上添加一个图片,当加了下面一句之后,上面的baidu字就会被覆盖 //label->setPixmap(QPixmap("../aaa.png"));
//当选中了lable之后会,输出字符串 connect(label,&QLabel::linkActivated,[](QStringstr){ qDebug()<<str; });
lay->addWidget(button=newQPushButton("Button")); //为button添加样式 button->setStyleSheet("QPushButton{font:bold16px;color:red;padding:5px}");
lay->addWidget(radio=newQRadioButton("Radio")); radio->setStyleSheet("QRadioButton{font:bold16px;color:red;padding:5px}"); //为单选按钮添加事件,true或者false connect(radio,&QRadioButton::clicked,[](boolv){ qDebug()<<v; });
lay->addWidget(newQCheckBox("checkbox")); //添加下拉选 lay->addWidget(combo=newQComboBox()); combo->addItem("Selectitem1"); combo->addItem("Selectitem2"); //设置下拉选可以被编辑 combo->setEditable(true); //当下拉选selectChanged之后,用信号槽进行处理 connect(combo,SIGNAL(currentIndexChanged(QString)),this,SLOT(slotComboBoxIndexChanged(QString))); combo->setCompleter(newQCompleter(combo->model()));
//说明多行文本框之中可以使用html标签页面 lay->addWidget(edit=newQTextEdit); edit->setText("<tableborder=1><tr><th>head1</th><th>head2</th></tr>" "<tr><td>value1</td><td>value2</td></tr>" "<tr><td>value3</td><td>value4</td></tr>" "</table><br>" "<imgsrc=http://www.mamicode.com/aaa.png>"); _edit=edit; connect(edit,&QTextEdit::textChanged,[&](){ // QTextEdit*edit=(QTextEdit*)this->sender(); qDebug()<<_edit->toPlainText(); }); //设置竖直方向上有水平滚动条 edit->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded); //设置水平方向上没有滚动条 edit->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
lay->addWidget(group=newQGroupBox("Someitems")); QHBoxLayout*hBoxLay; group->setLayout(hBoxLay=newQHBoxLayout); hBoxLay->addWidget(newQPushButton("aaaa")); hBoxLay->addWidget(newQPushButton("aaaa"));
QSlider*slider; QSpinBox*spinBox;
lay->addWidget(slider=newQSlider(Qt::Horizontal)); slider->setMaximum(100); slider->setMinimum(0);
lay->addWidget(spinBox=newQSpinBox); spinBox->setMaximum(100); spinBox->setMinimum(0);
connect(slider,SIGNAL(valueChanged(int)),spinBox, SLOT(setValue(int))); connect(spinBox,SIGNAL(valueChanged(int)),slider, SLOT(setValue(int)));
lay->addWidget(newQDateTimeEdit);
//设置LCD样式的显示状态 QLCDNumber*lcd; lay->addWidget(lcd=newQLCDNumber(10)); lcd->display(12345); lcd->setMode(QLCDNumber::Hex); lcd->setSegmentStyle(QLCDNumber::Outline); }
voidMyWidget::slotComboBoxIndexChanged(constQString&str) { qDebug()<<"slotComboBoxIndexChanged"<<str; }
intmain(intargc,char**argv) { QApplicationapp(argc,argv);
MyWidgetw; w.show();
returnapp.exec(); } |
运行结果: |
2.QLabel,QPushButton,QLineEdit,QComboBox,QCheckBox,QRadioButton,QTextEdit,QTextBrowser,QGroupBox,QSl