01_Qt基本控件及三大布局

来源:互联网 发布:淘宝全球买手那些人 编辑:程序博客网 时间:2024/05/29 14:00

    • 一Qt的三大布局
    • 二Qt的控件
    • 三Qt的信号槽

一、Qt的三大布局

  1. QHBoxLayout:
    1. 水平显示布局,所有在其上面摆放的控件只能水平排列下去;
  2. QVBoxLayout:
    1. 垂直显示布局,所有在其上面摆放的控件只能垂直排列下去;
  3. QGridLayout
    1. 格子显示布局,可以按照表格的形式显示布局;

例子:看下面代码的test1~4

二、Qt的控件

  1. label:标签,可以显示文本信息,只读;
  2. pushbutton : 普通按钮;
  3. radiobutton : 单选按钮,多个单选按钮中只能选择一个,但是必须放入groupbox中,类似单选题;
  4. checkbox : 多选复选按钮,可以选择同时选择多个,类似多选题;
  5. lineedit : 单行文本编辑框,可以输入单行文本;
  6. textedit : 多行文本输入框,可以输入显示多行文本和图片;
  7. combobox : 下拉文本输入框,在输入框的最右边有个三角下拉按钮,可以选择输入,也可以手动输入;
  8. textbrower : 多行文本显示框,只读;
  9. groupbox : 可以在里面放入特点的东西,统一管理;
  10. slider : 模拟显示滑动条;
  11. spinbox : 数值显示滑动条;
  12. dateedit :
  13. timeedit :
  14. datetimeedit :
  15. lcdnumber :

例子:看test5

三、Qt的信号槽

  1. 在Qt中所有的对象(继承QObject类)都有connect函数,只要有这个函数就能建立信号槽(通过触发某个控件的信号函数,执行槽中相应的函数);(暂时这样理解,还是有点理解不全面的,之后学习到再来修改);
  2. 在Qt中信号槽中可以使用自带的函数,四个参数;也可以执行自定义的函数,三个参数;具体看下面test4的例子就明白了。
#include <QtWidgets/QApplication>#include <QtCore/qdebug.h>#include <QtWidgets/qcompleter.h>#include <QtWidgets/qlineedit.h>#include <QtWidgets/qlabel.h>#include <QtWidgets/qcombobox.h>#include <QtWidgets/qcheckbox.h>#include <QtWidgets/qradiobutton.h>#include <QtWidgets/qtextedit.h>#include <QtWidgets/qtextbrowser.h>#include <QtWidgets/qgroupbox.h>#include <QtWidgets/qslider.h>#include <QtWidgets/qspinbox.h>#include <QtWidgets/qdatetimeedit.h>#include <QtWidgets/qtabwidget.h>#include <QtWidgets/qlcdnumber.h>#include <QtGui/qpixmap.h>#include <QtCore/qobject.h>#include <QtWidgets/qboxlayout.h>#include <QtWidgets/qpushbutton.h>#include "MyWidgetEvent.h"void test() ;int main(int argc, char *argv[]) {    QApplication a(argc, argv);    test() ;    return a.exec();}void test(){}void test5ManyKongJian() {    QWidget *nanWidget = new QWidget() ;    QVBoxLayout *nanVLayout = new QVBoxLayout() ;    /*    **测试label控件    */    QLabel *label=nullptr ;    nanVLayout->addWidget( label = new QLabel("<a href=www.baidu.com>baidu</a>") ) ;    //QPixmap me("./me.png") ;    //label->setPixmap( me ) ;//问题:链接和图片重复了,怎么分开    label->setWordWrap( true ) ;    label->adjustSize() ;    nanVLayout->connect( label , &QLabel::linkActivated , []( QString str){        qDebug()<<str ;    } ) ;    /*    **测试lineedit控件    */    QLineEdit *lineEdit ;    nanVLayout->addWidget( lineEdit = new QLineEdit("hello") ) ;    /*    **测试button控件    */    QPushButton *button ;    nanVLayout->addWidget( button = new QPushButton("???") ) ;    button->setStyleSheet("QPushButton {font:bold 16px; color:red;padding:5px}") ;    nanWidget->connect( button , &QPushButton::clicked , [](bool flag){        qDebug()<< "button" ;    }) ;    /*    **测试radiobutton控件    */    QRadioButton *radioButton ;    nanVLayout->addWidget( radioButton = new QRadioButton("qradiobutton") ) ;    radioButton->setStyleSheet("QRadioButton {font:bold  16px;color:blue;padding:5px}") ;    radioButton->connect( radioButton , &QRadioButton::clicked , [](bool flag){        qDebug()<< flag ;    }) ;    /*    **测试ckeckbox控件    */    QCheckBox *check ;    nanVLayout->addWidget( check = new QCheckBox("chekcbox") ) ;    /*    **测试combobox控件    */    QComboBox *combobox ;    nanVLayout->addWidget( combobox = new QComboBox() ) ;    combobox->addItem("select item1") ;    combobox->addItem("select item2") ;    combobox->addItem("...  ...") ;    combobox->setEditable(true) ;    lineEdit->setText("start") ;    combobox->connect( combobox , SIGNAL(activated(const QString &)) , lineEdit , SLOT(setText(const QString &)) ) ;//这里的下标要跟着显示    combobox->setCompleter( new QCompleter(combobox->model()) ) ;    /*    **测试textedit    */    QTextEdit *textEdit ;    nanVLayout->addWidget( textEdit = new QTextEdit("textedit") ) ;    textEdit->setText("<table border=2> "                    "<tr><td>good</td><td>good</td><td>good</td></tr>"                    "<tr><td>nice</td><td>nice</td><td>nice</td></tr>"                    "</table>"                    "<img src=./me.png></img>") ;    textEdit->connect( textEdit , &QTextEdit::textChanged,[&](){        //问题:textEdit->toPlainText() ;怎么才能获取到textEdit的实体,this->sender()        //QTextEdit _edit = (QTextEdit *)QObject::sender();        qDebug()<<textEdit->toPlainText() ;    }) ;    textEdit->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded) ;    textEdit->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOn) ;    /*    **测试groupbox控件    */    QGroupBox *groupBox ;    nanVLayout->addWidget( groupBox = new QGroupBox("groupbox") ) ;    QHBoxLayout *hLayout ;    groupBox->setLayout( hLayout = new QHBoxLayout() ) ;    hLayout->addWidget( new QRadioButton("boy") ) ;    hLayout->addWidget( new QRadioButton("girl") ) ;    /*    **测试模拟显示条slider    */    QSlider *slider ;    nanVLayout->addWidget( slider = new QSlider() ) ;    slider->setMinimum( -100 ) ;    slider->setMaximum( 100 ) ;    /*    **测试数字显示条    */    QSpinBox *spinBox ;    nanVLayout->addWidget( spinBox = new QSpinBox() ) ;    spinBox->setMinimum( -100 ) ;    spinBox->setMaximum( 100 ) ;    slider->connect( slider , SIGNAL(valueChanged(int)) , spinBox , SLOT(setValue(int)) ) ;    spinBox->connect( spinBox , SIGNAL(valueChanged(int)) , slider , SLOT(setValue(int)) ) ;    /*    **测试时间设置    */    nanVLayout->addWidget( new QDateTimeEdit() ) ;    /*    **测试显示时间,只读    */    QLCDNumber *number ;    nanVLayout->addWidget( number = new QLCDNumber() ) ;    number->display("1314") ;    number->setMode(QLCDNumber::Hex) ;    number->setSegmentStyle(QLCDNumber::Filled) ;    nanWidget->setLayout( nanVLayout ) ;    nanWidget->setWindowTitle("control") ;    nanWidget->show() ;}void test4GridAndHBox() {    QWidget *nanWidget = new QWidget() ;    QGridLayout *nanGridLayout = new QGridLayout() ;    QHBoxLayout *nanHBoxLayout = new QHBoxLayout() ;    nanGridLayout->addWidget( new QLabel("Username") , 1 , 1 ) ;    nanGridLayout->addWidget( new QLineEdit() , 1 , 2 ) ;    nanGridLayout->addWidget( new QLabel("Username") , 2 , 1 ) ;    nanGridLayout->addWidget( new QLineEdit() , 2 , 2 ) ;    nanGridLayout->addLayout( nanHBoxLayout , 3 , 2 ) ;    nanHBoxLayout->addStretch(1) ;    nanHBoxLayout->addWidget( new QPushButton("Login") , 1 ) ;    nanWidget->setLayout( nanGridLayout ) ;    nanWidget->show() ;}void test3GridLayout() {    QWidget *nanWidget = new QWidget() ;    QGridLayout *nanLayout = new QGridLayout() ;    QPushButton *nanButton = new QPushButton() ;    QLineEdit *nanLineEdit = new QLineEdit() ;    nanLayout->addWidget( nanLineEdit , 1 , 1 , 1 , 2 ) ;    nanLayout->addWidget( new QPushButton , 1, 3 ) ;    nanLayout->addWidget( new QLineEdit , 2, 1 , 2 , 1  ) ;     nanLayout->addWidget( new QPushButton , 2, 2 ) ;    nanLayout->addWidget( new QPushButton , 2, 3 ) ;    nanLayout->addWidget( nanButton , 3 , 3 ) ;    nanLayout->setColumnStretch( 1 , 1 ) ;    nanLayout->setColumnStretch( 2 , 1 ) ;/*设置每列的比重*/    nanLayout->setColumnStretch( 3 , 1 ) ;    nanWidget->setLayout( nanLayout ) ;    nanWidget->show() ;}void test2HBoxLayout() {    QWidget *nanQWidget = new QWidget() ;    QLineEdit *nanQLineEdit = new QLineEdit() ;    QHBoxLayout *nanHLayout = new QHBoxLayout() ;    nanHLayout->addWidget( nanQLineEdit , 1 ) ;    //添加,两个控件之间的距离addspaceing,两个控件在layout中的比重addstretch    nanQWidget->setLayout( nanHLayout ) ;    nanQWidget->show() ;} void test1(){    QWidget *w  = new QWidget ;    QVBoxLayout *vLayout = new QVBoxLayout( ) ;    QPushButton *nanButton ;    QLineEdit *nanLineEdit ;    QLabel *nanLabel ;    QString content("null") ;    QCompleter nanQCompleter( QStringList()<<"nich"<<"chen"<<"good") ;    vLayout->addWidget( nanLineEdit = new QLineEdit()  ) ;    vLayout->addWidget( nanButton = new QPushButton("right") ) ;    nanQCompleter.setFilterMode( Qt::MatchFlag::MatchContains ) ;    nanLineEdit->setCompleter( &nanQCompleter ) ;    nanLineEdit->setPlaceholderText( "Please input your name" ) ;    vLayout->addWidget(  nanLabel = new QLabel() ) ;    nanLabel->setText( content ) ;    w->connect( nanButton , &QPushButton::clicked , [&](){        nanLabel->setText( nanLineEdit->text() ) ;    } ) ;    w->setLayout( vLayout) ;    w->show() ;}
原创粉丝点击