网站建设擎宇,云服务器建设简易网站,安康微信公众平台,网站后台难做吗Qt里面有很多控件#xff0c;让我们来看一些常用控件。
首先是对pro文件的配置 HEADERS \ MyWidget.h SOURCES \ MyWidget.cpp QTwidgets gui CONFIG c11
因为要用到lambda所以要加一个CONFIGc11
下面是MyWidget.h
#ifndef MYWIDGET_H
#define MYWIDGET_H#include 让我们来看一些常用控件。
首先是对pro文件的配置 HEADERS \ MyWidget.h SOURCES \ MyWidget.cpp QTwidgets gui CONFIG c11
因为要用到lambda所以要加一个CONFIGc11
下面是MyWidget.h
#ifndef MYWIDGET_H
#define MYWIDGET_H#include QWidget
#include QTextEdit
class MyWidget : public QWidget
{Q_OBJECT
public:explicit MyWidget(QWidget *parent 0);signals:public slots://槽函数void comboChanged(const QString);
protected:QTextEdit *edit1;
};#endif // MYWIDGET_H下面是MyWidget.cpp #include MyWidget.h
#include QApplication
#include QLabel//显示静态文本或者图片
#include QPushButton
#include QLineEdit//一行
#include QComboBox
#include QCheckBox
#include QRadioButton
#include QTextEdit//富文本
#include QTextBrowser//只读富文本
#include QGroupBox//分类框
#include QSlider //滑块
#include QSpinBox //数字的
#include QDateEdit
#include QTimeEdit
#include QDateTimeEdit
#include QVBoxLayout
#include QCompleter
#include QHBoxLayout
#include QDebug
#include QPixmap
#include QLCDNumber
MyWidget::MyWidget(QWidget *parent) : QWidget(parent)
{//垂直布局QVBoxLayout* lay new QVBoxLayout(this);//一个labelQLabel* label;//一个下拉框QComboBox *combo;//一个按钮QPushButton* button;//一个点选框QRadioButton* radio;//一个富文本QTextEdit* edit;//一个组QGroupBox* group;//Qt里面可以用html来对控件的样式做变化lay-addWidget(labelnew QLabel(a hrefwww.baidu.combaidu/a));//设置按钮背景图片label-setPixmap(QPixmap(../bbb.jpg));//c11里面的新特性相对于槽模式lambda更加方便connect(label,QLabel::linkActivated,[](QString str){qDebug() str;});lay-addWidget(buttonnew QPushButton(Button));//设置css样式表,可见qt还是比较强大的button-setStyleSheet(QPushButton {font:bold 16px;color:red});lay-addWidget(radionew QRadioButton(Radio));radio-setStyleSheet(QRadioButton {font:bold 16px;color:red});lay-addWidget(new QCheckBox(check));connect(radio,QRadioButton::clicked,[](bool v){qDebug() v;});//添加下拉框lay-addWidget(combonew QComboBox());//添加2个元素combo-addItem(select item1);combo-addItem(select item2);//设置是否可编辑combo-setEditable(true);//注释掉的部分是自动补全的提示// combo-setCompleter(new QCompleter(QStringList() aaa bbb));//槽模式注意在别的地方有相应的函数connect(combo,SIGNAL(currentIndexChanged(QString)),this,SLOT(comboChanged(QString)));//设置自动补全提示内容为前面的itemcombo-setCompleter(new QCompleter(combo-model()));//添加富文本lay-addWidget(editnew QTextEdit);edit-setText(table border1trthheader1/ththheader2/th/trtrtdvalue1/tdtdvalue2/td/trtrtdvalue3/tdtdvalue4/td/tr/tablebr /img src../aaa.jpg/img);lay-addWidget(edit1new QTextEdit);connect(edit1,QTextEdit::textChanged,[](){qDebug() edit1-toPlainText();});//添加组lay-addWidget(groupnew QGroupBox(some items));//水平布局QHBoxLayout* layout;//把组的布局设置为水平布局group-setLayout(layoutnew QHBoxLayout);layout-addWidget(new QPushButton(aaaa));layout-addWidget(new QPushButton(aaaa));//把组加入到最开始的垂直布局里面lay-addWidget(new QGroupBox);//滑块QSlider* slider;lay-addWidget(slidernew QSlider(Qt::Horizontal));//设置滑块的最小值slider-setMinimum(0);//设置滑块的最大值slider-setMaximum(100);//spinbox类似于滑块不过其会显示值QSpinBox* spinBox;lay-addWidget(spinBoxnew QSpinBox);spinBox-setMaximum(100);spinBox-setMinimum(0);//槽函数把slider点的值给spinBoxconnect(slider,SIGNAL(valueChanged(int)),spinBox,SLOT(setValue(int)));lay-addWidget(new QDateTimeEdit);//LCDNumber显示LCD数字QLCDNumber* number;lay-addWidget(numbernew QLCDNumber(10));//显示12345number-display(12345);//显示风格number-setSegmentStyle(QLCDNumber::Flat);
}void MyWidget::comboChanged(const QString str)
{qDebug() combo value is str;
}int main(int argc,char** argv)
{QApplication app(argc,argv);MyWidget w;w.show();return app.exec();
}我已经对其进行了解释。
然后接下来我们看看显示效果有点逗敬请谅解。