QT学习笔记2(各种组件和四大布局)

我就是我 2022-12-24 11:55 457阅读 0赞

1、操作

新建一个项目:

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3d0bDE5OTI_size_16_color_FFFFFF_t_70

目录文件:

20201128060632510.png

2、窗口最大化、最小化和全屏

使窗口最大化、最小化和全屏:

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3d0bDE5OTI_size_16_color_FFFFFF_t_70 1

代码:

  1. //处理最大化按钮的事件
  2. QPushButton * maxPushButton = new QPushButton(this);
  3. maxPushButton->setText("最大化");
  4. maxPushButton->setGeometry(100,100,80,30);
  5. this->connect(maxPushButton,&QPushButton::clicked,[=](){
  6. this->setWindowState(Qt::WindowMaximized);
  7. });
  8. //处理最小化按钮的事件
  9. QPushButton * minPushButton = new QPushButton(this);
  10. minPushButton->setText("最小化");
  11. minPushButton->setGeometry(200,100,80,30);
  12. this->connect(minPushButton,&QPushButton::clicked,[=](){
  13. this->setWindowState(Qt::WindowMinimized);
  14. });
  15. //处理全屏按钮的事件
  16. QPushButton * fullScreenPushButton = new QPushButton(this);
  17. fullScreenPushButton->setText("全屏");
  18. fullScreenPushButton->setGeometry(300,100,80,30);
  19. this->connect(fullScreenPushButton,&QPushButton::clicked,[=](){
  20. this->setWindowState(Qt::WindowFullScreen);
  21. });

3、窗口无边框

使窗口无边框:

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3d0bDE5OTI_size_16_color_FFFFFF_t_70 2

代码:

  1. //处理关闭窗口事件
  2. QPushButton * closePushButton = new QPushButton(this);
  3. closePushButton->setText("关闭窗口");
  4. closePushButton->setGeometry(230,200,80,30);
  5. this->connect(closePushButton,&QPushButton::clicked,this,&QWidget::close);
  6. //设置无边框窗口
  7. setFixedSize(600,500);
  8. setWindowFlags(Qt::FramelessWindowHint);

4、QLabel的使用

4.1、显示文字

  1. QLabel * label = new QLabel(this);
  2. label->setText("我是label组件");

4.2、换行显示

  1. QLabel * label = new QLabel(this);
  2. label->setText("我是label组件\ntest");

4.3、设置样式:字体、字体颜色、背景

  1. setFixedSize(600,500);
  2. QLabel * label = new QLabel(this);
  3. label->setGeometry(200,200,100,60);
  4. label->setText("我是label组件\ntest");
  5. label->setStyleSheet(QString("background-color:#FF0000;"
  6. "color:#FFFFFF;"));
  7. label->setAlignment(Qt::AlignCenter);

4.4、显示图片、播放gif动画

4.4.1、用qss来进行设置background-image、border-image、image

  1. setFixedSize(600,500);
  2. QLabel * label = new QLabel(this);
  3. label->setGeometry(0,0,600,500);
  4. label->setText("我是label组件\ntest");
  5. // label->setStyleSheet(QString("background-image: url(':/resource/index_logo.png') no-repeat;"));
  6. // label->setStyleSheet(QString("image: url(':/resource/index_logo.png');"));
  7. label->setStyleSheet(QString("border-image: url(':/resource/index_logo.png');"));
  8. label->setAlignment(Qt::AlignCenter);

4.4.2、用QPixmap显示图片

  1. setFixedSize(600,500);
  2. QLabel * label = new QLabel(this);
  3. label->setGeometry(0,0,600,500);
  4. label->setText("我是label组件\ntest");
  5. // label->setStyleSheet(QString("background-image: url(':/resource/index_logo.png') no-repeat;"));
  6. // label->setStyleSheet(QString("image: url(':/resource/index_logo.png');"));
  7. // label->setStyleSheet(QString("border-image: url(':/resource/index_logo.png');"));
  8. label->setAlignment(Qt::AlignCenter);
  9. QPixmap pixmap = QPixmap(":/resource/index_logo.png");
  10. label->setWordWrap(true);
  11. label->setPixmap(pixmap);

4.4.3、播放gif动画

  1. setFixedSize(600,500);
  2. QLabel * label = new QLabel(this);
  3. label->setGeometry(0,0,600,500);
  4. label->setText("我是label组件\ntest");
  5. // label->setStyleSheet(QString("background-image: url(':/resource/index_logo.png') no-repeat;"));
  6. // label->setStyleSheet(QString("image: url(':/resource/index_logo.png');"));
  7. // label->setStyleSheet(QString("border-image: url(':/resource/index_logo.png');"));
  8. label->setAlignment(Qt::AlignCenter);
  9. QMovie * movie = new QMovie(":/resource/test.gif");
  10. label->setMovie(movie);
  11. label->show();
  12. movie->start();

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3d0bDE5OTI_size_16_color_FFFFFF_t_70 3

4.5、设置富文本(label->setTextFormat(Qt::RichText);)

  1. #include "mylabel.h"
  2. MyLabel::MyLabel(QWidget *parent) : QLabel(parent)
  3. {
  4. }
  5. void MyLabel::enterEvent(QEvent *event){
  6. this->setStyleSheet("background-color:#00FFFF;");
  7. }
  8. void MyLabel::leaveEvent(QEvent *event){
  9. this->setStyleSheet("background-color:#FF0000;");
  10. }
  11. setFixedSize(600,500);
  12. MyLabel * label = new MyLabel(this);
  13. label->setGeometry(0,0,600,500);
  14. label->setText("我是label组件<br/>test<br/>"
  15. "<a href='https://www.baidu.com'>百度一下</a>");
  16. label->setTextFormat(Qt::RichText);
  17. label->setAlignment(Qt::AlignCenter);
  18. label->setStyleSheet(QString("background-color:#FF0000;"
  19. "color:#FFFFFF;"));

4.6、选择和编辑

  1. setFixedSize(600,500);
  2. QLabel * label = new QLabel(this);
  3. label->setGeometry(0,0,600,500);
  4. label->setTextInteractionFlags(Qt::LinksAccessibleByKeyboard|Qt::LinksAccessibleByMouse|Qt::TextBrowserInteraction|Qt::TextEditable|Qt::TextEditorInteraction|Qt::TextSelectableByKeyboard|Qt::TextSelectableByMouse);
  5. label->setText("我是一个label标签<b/>"
  6. "<a href='https://www.baidu.com'>百度一下</a>");
  7. label->setTextFormat(Qt::RichText);
  8. label->setStyleSheet("background-color:#FF0000;"
  9. "color:#FFFFFF;");
  10. label->setAlignment(Qt::AlignCenter);

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3d0bDE5OTI_size_16_color_FFFFFF_t_70 4

5、QPushButton的使用

5.1、事件设置

  1. setMinimumSize(600,500);
  2. QPushButton * closePushButton = new QPushButton(this);
  3. closePushButton->setGeometry(230,200,100,36);
  4. closePushButton->setText("关闭窗口按钮");
  5. this->connect(closePushButton,&QPushButton::clicked,[=](){
  6. this->close();
  7. });
  8. QPushButton * maxPushButton = new QPushButton(this);
  9. maxPushButton->setGeometry(340,200,100,36);
  10. maxPushButton->setText("最大化窗口按钮");
  11. this->connect(maxPushButton,&QPushButton::released,[=](){
  12. this->setWindowState(Qt::WindowMaximized);
  13. });

5.2、快捷键设置

  1. setMinimumSize(600,500);
  2. QPushButton * closePushButton = new QPushButton(this);
  3. closePushButton->setGeometry(230,200,100,36);
  4. closePushButton->setText("关闭窗口按钮");
  5. this->connect(closePushButton,&QPushButton::clicked,[=](){
  6. this->close();
  7. });
  8. QPushButton * maxPushButton = new QPushButton(this);
  9. maxPushButton->setGeometry(340,200,100,36);
  10. maxPushButton->setText("最大化窗口按钮");
  11. this->connect(maxPushButton,&QPushButton::released,[=](){
  12. this->setWindowState(Qt::WindowMaximized);
  13. });
  14. maxPushButton->setShortcut(tr("Ctrl+E"));

5.3、样式设置

QPushButton:hover、QPushButton:!hover:

  1. setMinimumSize(600,500);
  2. QPushButton * closePushButton = new QPushButton(this);
  3. closePushButton->setGeometry(230,200,200,36);
  4. closePushButton->setText("关闭窗口按钮");
  5. closePushButton->setStyleSheet("QPushButton:hover{"
  6. "background-color : #FF0000;"
  7. "color:#00FFFF;"
  8. "font-size:30px;"
  9. "}"
  10. ""
  11. "QPushButton:!hover{"
  12. "background-color : #FFFF00;"
  13. "color:#0000FF;"
  14. "font-size:20px;"
  15. "}"
  16. ""
  17. "background-color:#FFFF00;"
  18. "color:#0000FF;"
  19. "font-size:20px;");

6、QLineEdit的使用

6.1、属性方法

  1. setPlaceholderText:设置输入提示
  2. setFont:设置字体
  3. setReadOnly:设置是否可复制
  4. setMaxLength:设置输入字数的最大长度
  5. setEchoMode:设置输入框的模式:Normal, NoEcho, Password, PasswordEchoOnEdit

20201129094414491.png

  1. setMinimumSize(600,500);
  2. lineEdit = new QLineEdit(this);
  3. lineEdit->setGeometry(230,200,200,50);
  4. lineEdit->setPlaceholderText("请输入一行字符串");
  5. lineEdit->setFont(QFont("微软雅黑",16));
  6. lineEdit->setReadOnly(false);
  7. lineEdit->setMaxLength(10);
  8. lineEdit->setEchoMode(QLineEdit::Password);

6.2、信号事件

  1. setMinimumSize(600,500);
  2. lineEdit = new QLineEdit(this);
  3. lineEdit->setGeometry(230,200,200,50);
  4. lineEdit->setPlaceholderText("请输入一行字符串");
  5. lineEdit->setFont(QFont("微软雅黑",16));
  6. lineEdit->setReadOnly(false);
  7. lineEdit->setMaxLength(3);
  8. QPushButton * undoPushButton = new QPushButton(this);
  9. undoPushButton->setGeometry(450,200,200,50);
  10. undoPushButton->setText("undo操作");
  11. this->connect(undoPushButton,&QPushButton::clicked,this,&Widget::dealClick);

6.3、qss样式

  1. lineEdit->setStyleSheet("QLineEdit{"
  2. "border:2px solid red;"
  3. "border-radius:2px;"
  4. "}"
  5. ""
  6. "QLineEdit[echoMode='2']{"
  7. "background-color:#CCCCCC;"
  8. "}");

7、QObject的解读和布局类(Layout)

7.1、递归遍历子节点(children())

  1. void Widget::listChildrens(QWidget * widget){
  2. QObjectList objectList = widget->children();
  3. for (int i=0;i<objectList.size();i++){
  4. qDebug() << objectList[i]->objectName();
  5. qDebug() << objectList[i]->metaObject()->className();
  6. listChildrens(qobject_cast<QWidget *>(objectList[i]));
  7. }
  8. }
  9. setMinimumSize(600,500);
  10. QPushButton * pushButton = new QPushButton(this);
  11. pushButton->setObjectName("pushButton");
  12. QCheckBox * checkBox = new QCheckBox(this);
  13. checkBox->setObjectName("checkBox");
  14. QLabel * label = new QLabel(this);
  15. label->setObjectName("label");
  16. listChildrens(this);

7.2、Layout

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3d0bDE5OTI_size_16_color_FFFFFF_t_70 5

setContentsMargins:设置整个布局的间隔;setSpacing:设置组件间隔

  1. QVBoxLayout * vBoxLayout = new QVBoxLayout(layoutWidget);
  2. vBoxLayout->setContentsMargins(0,0,0,0);
  3. vBoxLayout->setSpacing(0);

resize在layout里不起作用,应该用setMinimumSize进行大小的设置!!!

  1. label->setMinimumSize(100,100);

7.2.1、QVBoxLayout

  1. setMinimumSize(600,500);
  2. QWidget * layoutWidget = new QWidget(this);
  3. layoutWidget->setGeometry(0,0,200,200);
  4. layoutWidget->setStyleSheet("background-color: blue;");
  5. QVBoxLayout * vBoxLayout = new QVBoxLayout(layoutWidget);
  6. vBoxLayout->setContentsMargins(0,0,0,0);
  7. vBoxLayout->setSpacing(0);
  8. QPushButton * pushButton = new QPushButton(layoutWidget);
  9. pushButton->setObjectName("pushButton");
  10. pushButton->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Fixed);
  11. pushButton->setStyleSheet("background-color:green;margin:-1px;");
  12. QCheckBox * checkBox = new QCheckBox(layoutWidget);
  13. checkBox->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Fixed);
  14. checkBox->setObjectName("checkBox");
  15. checkBox->setStyleSheet("background-color:red;margin:-1px;");
  16. QLabel * label = new QLabel(layoutWidget);
  17. label->setObjectName("label");
  18. vBoxLayout->addWidget(pushButton);
  19. vBoxLayout->addWidget(checkBox);
  20. vBoxLayout->addWidget(label);

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3d0bDE5OTI_size_16_color_FFFFFF_t_70 6

7.2.2、QHBoxLayout

  1. setMinimumSize(600,500);
  2. QWidget * layoutWidget = new QWidget(this);
  3. layoutWidget->setGeometry(0,0,200,200);
  4. layoutWidget->setStyleSheet("background-color: blue;");
  5. QHBoxLayout * hBoxLayout = new QHBoxLayout(layoutWidget);
  6. hBoxLayout->setContentsMargins(0,0,0,0);
  7. hBoxLayout->setSpacing(0);
  8. hBoxLayout->setAlignment(Qt::AlignLeft);
  9. QPushButton * pushButton = new QPushButton(layoutWidget);
  10. pushButton->setObjectName("pushButton");
  11. pushButton->setSizePolicy(QSizePolicy::Fixed,QSizePolicy::Fixed);
  12. pushButton->setStyleSheet("background-color:green;margin:-1px;");
  13. QCheckBox * checkBox = new QCheckBox(layoutWidget);
  14. checkBox->setSizePolicy(QSizePolicy::Fixed,QSizePolicy::Fixed);
  15. checkBox->setObjectName("checkBox");
  16. checkBox->setStyleSheet("background-color:red;margin:-1px;");
  17. QLabel * label = new QLabel(layoutWidget);
  18. label->setObjectName("label");
  19. label->setSizePolicy(QSizePolicy::Fixed,QSizePolicy::Fixed);
  20. label->setMinimumSize(100,100);
  21. label->setStyleSheet("background-color:yellow;margin:-1px;");
  22. hBoxLayout->addWidget(pushButton);
  23. hBoxLayout->addWidget(checkBox);
  24. hBoxLayout->addWidget(label);

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3d0bDE5OTI_size_16_color_FFFFFF_t_70 7

7.2.3、QGridLayout

  1. gridLayout->setContentsMargins(0,0,0,0);
  2. gridLayout->setVerticalSpacing(0);
  3. gridLayout->setHorizontalSpacing(0);
  4. gridLayout->setSpacing(0);
  5. setMinimumSize(600,500);
  6. QWidget * layoutWidget = new QWidget(this);
  7. layoutWidget->setGeometry(0,0,200,200);
  8. layoutWidget->setStyleSheet("background-color: blue;");
  9. QGridLayout * gridLayout = new QGridLayout(layoutWidget);
  10. gridLayout->setContentsMargins(0,0,0,0);
  11. gridLayout->setVerticalSpacing(0);
  12. gridLayout->setHorizontalSpacing(0);
  13. gridLayout->setSpacing(0);
  14. QPushButton * pushButton1 = new QPushButton(layoutWidget);
  15. pushButton1->setSizePolicy(QSizePolicy::Fixed,QSizePolicy::Fixed);
  16. pushButton1->setMinimumSize(50,50);
  17. pushButton1->setText("button1");
  18. QPushButton * pushButton2 = new QPushButton(layoutWidget);
  19. pushButton2->setMinimumSize(50,50);
  20. pushButton2->setText("button2");
  21. QPushButton * pushButton3 = new QPushButton(layoutWidget);
  22. pushButton3->setMinimumSize(50,50);
  23. pushButton3->setText("button3");
  24. QPushButton * pushButton4 = new QPushButton(layoutWidget);
  25. pushButton4->setMinimumSize(50,50);
  26. pushButton4->setText("button4");
  27. gridLayout->addWidget(pushButton1,0,0,Qt::AlignCenter);
  28. gridLayout->addWidget(pushButton2,0,1,Qt::AlignCenter);
  29. gridLayout->addWidget(pushButton3,0,2,Qt::AlignCenter);
  30. gridLayout->addWidget(pushButton4,1,1,Qt::AlignCenter);

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3d0bDE5OTI_size_16_color_FFFFFF_t_70 8

现象:内部的组件设置SizePolicy为Fixed不生效,默认会充满整个layout!!!

7.2.4、QFormLayout

  1. setMinimumSize(600,500);
  2. QWidget * layoutWidget = new QWidget(this);
  3. layoutWidget->setGeometry(0,0,200,200);
  4. layoutWidget->setStyleSheet("background-color: blue;");
  5. QFormLayout * formLayout = new QFormLayout(layoutWidget);
  6. formLayout->setContentsMargins(0,0,0,0);
  7. formLayout->setVerticalSpacing(0);
  8. formLayout->setHorizontalSpacing(0);
  9. formLayout->setSpacing(0);
  10. QPushButton * pushButton1 = new QPushButton(layoutWidget);
  11. pushButton1->setMinimumSize(50,50);
  12. pushButton1->setText("button1");
  13. QPushButton * pushButton2 = new QPushButton(layoutWidget);
  14. pushButton2->setMinimumSize(50,50);
  15. pushButton2->setText("button2");
  16. QPushButton * pushButton3 = new QPushButton(layoutWidget);
  17. pushButton3->setMinimumSize(50,50);
  18. pushButton3->setText("button3");
  19. QPushButton * pushButton4 = new QPushButton(layoutWidget);
  20. pushButton4->setMinimumSize(50,50);
  21. pushButton4->setText("button4");
  22. formLayout->insertRow(0,pushButton1,pushButton2);
  23. formLayout->insertRow(1,pushButton3,pushButton4);

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3d0bDE5OTI_size_16_color_FFFFFF_t_70 9

遍历QFormLayout:

  1. for (int i=0;i<formLayout->rowCount();i++){
  2. QLayoutItem * labelLayoutItem = formLayout->itemAt(i,QFormLayout::LabelRole);
  3. QLayoutItem * fieldLayoutItem = formLayout->itemAt(i,QFormLayout::FieldRole);
  4. QPushButton * labelPushButton = qobject_cast<QPushButton *>(labelLayoutItem->widget());
  5. QPushButton * fieldPushButton = qobject_cast<QPushButton *>(fieldLayoutItem->widget());
  6. qDebug() << labelPushButton->metaObject()->className();
  7. qDebug() << fieldPushButton->metaObject()->className();
  8. }

8、QCheckBox、QRadioButton、QButtonGroup的使用

8.1、QCheckBox

  1. setMinimumSize(600,500);
  2. QCheckBox * checkBox = new QCheckBox(this);
  3. checkBox->setText("音乐");
  4. checkBox->setGeometry(100,100,100,30);

8.2、QButtonGroup结合QCheckBox使用

  1. setMinimumSize(600,500);
  2. QButtonGroup * buttonGroup = new QButtonGroup(this);
  3. QCheckBox * checkBox1 = new QCheckBox(this);
  4. checkBox1->setText("音乐");
  5. checkBox1->setGeometry(100,100,100,30);
  6. QCheckBox * checkBox2 = new QCheckBox(this);
  7. checkBox2->setText("水果");
  8. checkBox2->setGeometry(100,140,100,30);
  9. QCheckBox * checkBox3 = new QCheckBox(this);
  10. checkBox3->setText("蔬菜");
  11. checkBox3->setGeometry(100,180,100,30);
  12. buttonGroup->addButton(checkBox1);
  13. buttonGroup->addButton(checkBox2);
  14. buttonGroup->addButton(checkBox3);
  15. //设置是否为单选 true:单选 false:多选
  16. buttonGroup->setExclusive(true);

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3d0bDE5OTI_size_16_color_FFFFFF_t_70 10

8.3、QButtonGroup结合QRadioButton使用

  1. setMinimumSize(600,500);
  2. QButtonGroup * buttonGroup = new QButtonGroup(this);
  3. QRadioButton * radioButton1 = new QRadioButton(this);
  4. radioButton1->setText("音乐");
  5. radioButton1->setGeometry(100,100,100,30);
  6. QRadioButton * radioButton2 = new QRadioButton(this);
  7. radioButton2->setText("水果");
  8. radioButton2->setGeometry(100,140,100,30);
  9. QRadioButton * radioButton3 = new QRadioButton(this);
  10. radioButton3->setText("蔬菜");
  11. radioButton3->setGeometry(100,180,100,30);
  12. buttonGroup->addButton(radioButton1);
  13. buttonGroup->addButton(radioButton2);
  14. buttonGroup->addButton(radioButton3);
  15. radioButton2->setChecked(true);
  16. //设置是否为单选 true:单选 false:多选
  17. buttonGroup->setExclusive(true);

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3d0bDE5OTI_size_16_color_FFFFFF_t_70 11

信号处理:

  1. setMinimumSize(600,500);
  2. QButtonGroup * buttonGroup = new QButtonGroup(this);
  3. QRadioButton * radioButton1 = new QRadioButton(this);
  4. radioButton1->setObjectName("radioButton1");
  5. radioButton1->setText("音乐");
  6. radioButton1->setGeometry(100,100,100,30);
  7. QRadioButton * radioButton2 = new QRadioButton(this);
  8. radioButton2->setObjectName("radioButton2");
  9. radioButton2->setText("水果");
  10. radioButton2->setGeometry(100,140,100,30);
  11. QRadioButton * radioButton3 = new QRadioButton(this);
  12. radioButton3->setObjectName("radioButton3");
  13. radioButton3->setText("蔬菜");
  14. radioButton3->setGeometry(100,180,100,30);
  15. buttonGroup->addButton(radioButton1,1);
  16. buttonGroup->addButton(radioButton2,2);
  17. buttonGroup->addButton(radioButton3,3);
  18. radioButton2->setChecked(true);
  19. //设置是否为单选 true:单选 false:多选
  20. buttonGroup->setExclusive(true);
  21. this->connect(buttonGroup,QOverload<QAbstractButton *>::of(&QButtonGroup::buttonClicked),[=](QAbstractButton * button){
  22. QRadioButton * radioButton = qobject_cast<QRadioButton *>(button);
  23. qDebug() << radioButton->isChecked() << radioButton->objectName();
  24. });

8、QComboBox(下拉框)

  1. setMinimumSize(600,500);
  2. QComboBox * comBox = new QComboBox(this);
  3. comBox->setGeometry(200,200,200,50);
  4. comBox->setIconSize(QSize(50,50));
  5. comBox->addItem(QIcon(":/resource/index_logo.png"),QString("test1"),QVariant("data1"));
  6. comBox->addItem(QIcon(":/resource/index_logo.png"),QString("test2"),QVariant("data2"));
  7. comBox->addItem(QIcon(":/resource/index_logo.png"),QString("test3"),QVariant("data3"));
  8. //处理信号与槽
  9. this->connect(comBox,QOverload<int>::of(&QComboBox::activated),[=](int index){
  10. qDebug() << index << comBox->itemText(index) << comBox->itemData(index);
  11. });

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3d0bDE5OTI_size_16_color_FFFFFF_t_70 12

9、QSlider(滑块)

  1. setMinimumSize(600,500);
  2. QSlider * slider = new QSlider(this);
  3. slider->setRange(0,100);
  4. slider->setGeometry(200,200,200,50);
  5. slider->setOrientation(Qt::Horizontal);
  6. slider->setStyleSheet("QSlider:groove{"
  7. "background-color:red;"
  8. "}"
  9. ""
  10. "QSlider:handle{"
  11. "background-color:blue;"
  12. "width:6px;"
  13. "}");
  14. this->connect(slider,&QSlider::sliderMoved,[=](int pos){
  15. qDebug() << pos;
  16. });

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3d0bDE5OTI_size_16_color_FFFFFF_t_70 13

9、QListWidge

9.1、基本操作

  1. setMinimumSize(600,500);
  2. QListWidget * listWidget = new QListWidget(this);
  3. listWidget->setGeometry(200,50,360,300);
  4. listWidget->setCurrentRow(10);
  5. listWidget->setSortingEnabled(false);
  6. listWidget->setIconSize(QSize(50,50));
  7. //设置Ctrl多选
  8. listWidget->setSelectionMode(QAbstractItemView::ExtendedSelection);
  9. for (int i=0;i<100;i++){
  10. QListWidgetItem * listWidgetItem = new QListWidgetItem(QIcon(":/resource/index_logo.png"),QString("item%1").arg(i+1),listWidget);
  11. listWidget->addItem(listWidgetItem);
  12. }
  13. //设置信号和槽
  14. this->connect(listWidget,&QListWidget::itemClicked,[=](QListWidgetItem *item){
  15. qDebug() << item->text();
  16. });

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3d0bDE5OTI_size_16_color_FFFFFF_t_70 14

9.2、设置listWidget里的子项可编辑

  1. for (int i=0;i<100;i++){
  2. QListWidgetItem * listWidgetItem = new QListWidgetItem(QIcon(":/resource/index_logo.png"),QString("item%1").arg(i+1),listWidget);
  3. //设置listWidget里的子项可编辑
  4. listWidgetItem->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEditable | Qt::ItemIsEnabled);
  5. listWidget->addItem(listWidgetItem);
  6. }

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3d0bDE5OTI_size_16_color_FFFFFF_t_70 15

9.3、遍历listWidget里的所有子项

  1. for (int i=0;i<listWidget->count();i++){
  2. QListWidgetItem * listWidgetItem = listWidget->item(i);
  3. qDebug() << listWidgetItem->text();
  4. }

9.4、对listWidget列表排序

  1. listWidget->sortItems(Qt::DescendingOrder);

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3d0bDE5OTI_size_16_color_FFFFFF_t_70 16

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3d0bDE5OTI_size_16_color_FFFFFF_t_70 17

9.5、在QListWidget上添加组件

  1. for (int i=0;i<100;i++){
  2. QListWidgetItem * listWidgetItem = new QListWidgetItem(QIcon(":/resource/index_logo.png"),QString("item%1").arg(i+1),listWidget);
  3. //设置listWidget里的子项可编辑
  4. listWidgetItem->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEditable | Qt::ItemIsEnabled);
  5. listWidget->addItem(listWidgetItem);
  6. }
  7. listWidget->sortItems(Qt::DescendingOrder);
  8. listWidget->sortItems(Qt::DescendingOrder);
  9. listWidget->setItemWidget(listWidget->item(0),new QPushButton(listWidget));

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3d0bDE5OTI_size_16_color_FFFFFF_t_70 18

9、QListWidge

9.1、设置滚动条策略

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3d0bDE5OTI_size_16_color_FFFFFF_t_70 19

  1. tableWidget->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
  2. tableWidget->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);

9.2、添加table

  1. setMinimumSize(600,500);
  2. QTableWidget * tableWidget = new QTableWidget(this);
  3. tableWidget->setGeometry(200,100,300,300);
  4. tableWidget->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
  5. tableWidget->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
  6. tableWidget->setColumnCount(3);
  7. for (int i=0;i<3;i++){
  8. QTableWidgetItem * tableWidgetItem = new QTableWidgetItem(QString("column%1").arg(i+1));
  9. tableWidget->setHorizontalHeaderItem(i,tableWidgetItem);
  10. }
  11. tableWidget->setRowCount(100);
  12. for (int i=0;i<100;i++){
  13. for (int j=0;j<tableWidget->columnCount();j++){
  14. QTableWidgetItem * tableWidgetItem = new QTableWidgetItem(QString("column%1").arg(j+1));
  15. tableWidget->setItem(i,j,tableWidgetItem);
  16. }
  17. }

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3d0bDE5OTI_size_16_color_FFFFFF_t_70 20

9.3、在QTableWidget上添加组件

  1. QPushButton * pushButton = new QPushButton(tableWidget);
  2. pushButton->setMinimumSize(200,30);
  3. pushButton->setText("按钮组件");
  4. tableWidget->setColumnWidth(0,300);
  5. tableWidget->setCellWidget(0,0,pushButton);

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3d0bDE5OTI_size_16_color_FFFFFF_t_70 21

9.4、设置行标

  1. QStringList * stringList = new QStringList();
  2. for (int i=0;i<100;i++){
  3. stringList->append(QString("row%1").arg(i+1));
  4. }
  5. tableWidget->setVerticalHeaderLabels(*stringList);

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3d0bDE5OTI_size_16_color_FFFFFF_t_70 22

9.5、removeRow

  1. tableWidget->removeRow(1);

发表评论

表情:
评论列表 (有 0 条评论,457人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Qt学习笔记-布局管理器

    在设计较复杂的GUI用户界面时,仅通过指定窗口部件的父子关系以期达到加载和排列窗口部件的方法是行不通的,最好的办法是使用Qt提供的布局管理器。 QGridLayout \ma