当前位置:   article > 正文

Qt5.12实战之控件设计_qt控件设计

qt控件设计
  1. QPushButton使用

静态创建,通过设计窗口拖放控件:

添加信号处理:

为Pressed,Released,Clicked三个按钮添加信号

自动添加的信号槽:

动态生成的槽定义:

动态创建创建QPushButton

  1. QPushButton *btn = new QPushButton(QString::fromLocal8Bit("动态添加的按钮"),this);
  2. connect(btn,SIGNAL(clicked()),this,SLOT(on_cust_dynbtn()));
  3. ui->gridLayout->addWidget(btn, 4, 0, 1, 1);

动态创建QToolButton

  1. QToolButton *tool = new QToolButton(this);
  2. tool->setArrowType(Qt::LeftArrow);
  3. tool->setText(QString::fromLocal8Bit("自动创建的工具按钮"));
  4. tool->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
  5. connect(tool,SIGNAL(clicked()),this,SLOT(on_cust_tool_clicked()));
  6. ui->gridLayout->addWidget(tool,5,0,1,1);

QRadioButton使用:

先在设计窗口调整好控件布局

使用代码添加到按钮组:

  1. //按钮组
  2. group = new QButtonGroup(this);
  3. group->addButton(ui->radioButton,0);
  4. group->addButton(ui->radioButton_2,1);
  5. group->addButton(ui->radioButton_3,2);
  6. ui->radioButton->setChecked(true);//默认项

对按钮组中的控件进行信号连接

  1. //信号连接
  2. connect(ui->radioButton,SIGNAL(clicked(bool)),this,SLOT(on_radiobtn_checked(bool)));
  3. connect(ui->radioButton_2,SIGNAL(clicked(bool)),this,SLOT(on_radiobtn_checked(bool)));
  4. connect(ui->radioButton_3,SIGNAL(clicked(bool)),this,SLOT(on_radiobtn_checked(bool)));

QCheckBox使用:

动态创建:

  1. strList1<<"One"<<"Two"<<"Three";
  2. //指向QCheckBox的指针
  3. QCheckBox *exclusive[3];
  4. //创建按钮组
  5. group = new QButtonGroup(this);
  6. //动态创建QCheckBox并添加到按钮组
  7. for (int i=0;i<strList1.size() ;i++ ) {
  8. exclusive[i] = new QCheckBox(strList1[i],this);
  9. //设置QCheckBox几何位置与控件大小
  10. //没有添加布局层的可用setGeometry
  11. //添加了布局层的只能用addWidget
  12. exclusive[i]->setGeometry(50,50+i*50,200,30);
  13. //信号连接
  14. connect(exclusive[i],SIGNAL(stateChanged(int)),this,SLOT(on_stateChanged(int)));
  15. group->addButton(exclusive[i],i);
  16. }
  17. group->setExclusive(true);//true:单选 false:复选

QListView使用:

  1. //初始化数据模型
  2. model = new QStringListModel(this);
  3. model->setStringList(strList1);
  4. listview=new QListView(this);
  5. //临时更新数据集
  6. QStringList tmp = model->stringList();
  7. tmp.append("Four");
  8. tmp<<"Fire"<<"Six";
  9. model->setStringList(tmp);
  10. listview->setModel(model);
  11. listview->setGeometry(50,200,300,300);
  12. listview->model()->removeRow(model->rowCount()-1);
  13. listview->model()->removeRows(3,2);

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/AllinToyou/article/detail/81652
推荐阅读
相关标签
  

闽ICP备14008679号