QT 常用控件操作实例集锦

小编:啊南 113阅读 2020.12.26

运行结果:

点击按钮1:

界面纯代码编写

以下是源码:

QtGuiApplication7.h

#pragma once

#include <QtWidgets/QMainWindow>
#include "ui_QtGuiApplication7.h"

class QtGuiApplication7 : public QMainWindow
{
  Q_OBJECT

public:
  QtGuiApplication7(QWidget *parent = Q_NULLPTR);

private:
  Ui::QtGuiApplication7Class ui;
private slots:
  void on_btn2_clicked();
  void on_btn1_clicked();
  void warning();

};

QtGuiApplication7.cpp

#include "QtGuiApplication7.h"
#include <qpushbutton.h>
#include <QMessageBox>
#include <qcombobox.h>
#include <qradiobutton.h>
#include <qlabel.h>
#include <qtableview.h>
#include <qstandarditemmodel.h>
#include <qtextedit.h>

#if _MSC_VER >= 1600
#pragma execution_character_set("utf-8")
#endif

QPushButton *btn1, *btn2;
QRadioButton *qbtn1, *qbtn2;

QtGuiApplication7::QtGuiApplication7(QWidget *parent)
  : QMainWindow(parent)
{
  ui.setupUi(this);
  /*实例化两个按钮,并建立信号与槽的关系*/
  btn1 = new QPushButton(this);
  btn1->setGeometry(20, 60, 80, 40);
  btn1->setText("测试1");
  btn2 = new QPushButton(this);
  btn2->setGeometry(120, 60, 80, 40);
  btn2->setText("测试2");
  /*实例化一个QComboBox,添加值后选择第三个值*/
  QComboBox *cmb = new QComboBox(this);
  //方法-
  cmb->addItem("选项1");
  cmb->addItem("选项2");
  cmb->addItem("选项3");
  //方法二
  QStringList QList;
  QList.clear();
  QList << tr("选项4") << tr("选项5") << tr("选项6") ;
  cmb->addItems(QList);
  cmb->setGeometry(20, 20, 70, 30);
  cmb->setCurrentIndex(2);
  /*实例化两个QRadioButton并选中第二个*/
  qbtn1 = new QRadioButton("男",this);
  qbtn1->setGeometry(120,30,50,20);
    qbtn2 = new QRadioButton("女",this);
  qbtn2->setGeometry(180, 30, 50, 20);
  qbtn2->setChecked(true);
  /*实例化两个Qlabel并命名*/
  QLabel *lab1 = new QLabel("标签1",this);
  lab1->setGeometry(20,100,50,50);
  QLabel *lab2 = new QLabel("标签2", this);
  lab2->setGeometry(120, 100, 50, 50);
  /*实例化textedit*/
  QTextEdit *tex = new QTextEdit("文本1",this);
  tex->setGeometry(230,20,70,30);
  /*实例化Qlabel,显示图片*/
  QLabel *lab3 = new QLabel("标签3", this);
  QPixmap pixmap("icon32/stop.png");
  lab3->setPixmap(pixmap);
  btn2->setIcon(pixmap);
  lab3->setGeometry(310, 30, 160, 30);
  lab3->setAlignment(Qt::AlignCenter);
  //lab3->setText("启动");

  /*实例化一个tableview*/
  QTableView *tab = new QTableView(this);
  QStandardItemModel *model = new QStandardItemModel;
  model->setColumnCount(5);
  tab->setModel(model);
  model->setHeaderData(0, Qt::Horizontal, QString("File"));
  model->setHeaderData(1, Qt::Horizontal, QString("Path"));
  model->setHeaderData(2, Qt::Horizontal, QString("Size"));
  model->setHeaderData(3, Qt::Horizontal, QString("Duration"));
  model->setHeaderData(4, Qt::Horizontal, QString("value"));
  tab->setShowGrid(true); //显示格子
  tab->setColumnWidth(0, 80);
  tab->setColumnWidth(1, 50);
  tab->setColumnWidth(2, 80);
  tab->setColumnWidth(3, 80);
  tab->setColumnWidth(3, 80);
  tab->setGeometry(20,110,400,200);

  for (int i = 0; i < 3; i++)
  {
    model->setItem(i, 0, new QStandardItem("2009441676"));
    //设置字符颜色 
    model->item(i, 0)->setForeground(QBrush(QColor(255, 0, 0)));
    //设置字符位置 
    model->item(i, 0)->setTextAlignment(Qt::AlignCenter);
    //model->setItem(i, 1, new QStandardItem(QString::fromLocal8Bit("哈哈")));
    model->setItem(i, 1, new QStandardItem(QString("哈哈")));
  }

  this->resize(440,330);
  connect(btn2, SIGNAL(clicked()), this, SLOT(on_btn2_clicked()));
  connect(btn1, SIGNAL(clicked()), this, SLOT(on_btn1_clicked()));
}
void QtGuiApplication7::on_btn2_clicked()
{
  warning();
}
void QtGuiApplication7::on_btn1_clicked()
{
  warning();
}
void QtGuiApplication7::warning()
{
  if (qbtn2->isChecked()) //()不要忽略
    QMessageBox::information(this, "提醒", "当前选择的性别是女");
  else
    QMessageBox::information(this, "提醒", "当前选择的性别是男");
}
关联标签: