Qt学习六:控件三:单选按钮、复选框、列表控件、树控件
2016-06-27 14:01
706 查看
这是第三波控件描述;
首先,给出qtButton.h的程序如下:
其次,再给出qtButton.cpp中的程序:
#include "qtbutton.h"
qtButton::qtButton(QWidget *parent)
: QMainWindow(parent)
{
ui.setupUi(this);
//----------------RadioButton---------------
radioM = new QRadioButton(this);
radioW = new QRadioButton(this);
label = new QLabel(this);
radioM -> setGeometry(QRect(50, 50, 50, 50));
radioW->setGeometry(QRect(100, 50, 50, 50));
label->setGeometry(QRect(50, 100, 100, 25));
radioM->setText("Man");
radioW->setText("Woman");
//default values;
radioM->setChecked(true);
label->setText("Man");
connect(radioM, SIGNAL(clicked()), this, SLOT(radioChange()));
connect(radioW, SIGNAL(clicked()), this, SLOT(radioChange()));
//---------------CheckBox------------
checkBox01 = new QCheckBox(this);
checkBox02 = new QCheckBox(this);
checkBox03 = new QCheckBox(this);
label02 = new QLabel(this);
checkBox01->setGeometry(QRect(50, 150, 50, 50));
checkBox02->setGeometry(QRect(100, 150, 100, 50));
checkBox03->setGeometry(QRect(170, 150, 100, 50));
label02->setGeometry(QRect(50, 200, 200, 30));
label02->setText("initial");
checkBox01->setText("Math");
checkBox02->setText("Chinese");
checkBox03->setText("Geometry");
connect(checkBox01, SIGNAL(clicked(bool)), this, SLOT(checkChange()));
connect(checkBox02, SIGNAL(clicked(bool)), this, SLOT(checkChange()));
connect(checkBox03, SIGNAL(clicked(bool)), this, SLOT(checkChange()));
//----------ListView---------------
listView = new QListView(this);
listView->setGeometry(50, 250, 130, 100);
QStringList string;
string << "Math" << "Lauguage" << "Foreign Language" << "GeoMetry";
model = new QStringListModel(string);
listView->setModel(model);
//-----------QTreeView----------------------
treeView = new QTreeView(this);
treeView->setGeometry(QRect(200, 250, 300, 200));
standardModel = new QStandardItemModel(3, 2);
standardModel->setHeaderData(0, Qt::Horizontal, "The First List");
standardModel->setHeaderData(1, Qt::Horizontal, "The Second List");
QStandardItem *item01 = new QStandardItem("Math");
item01->setIcon(QIcon(":/new/profix1/folder"));
QStandardItem *item02 = new QStandardItem("Chinese");
item02->setIcon(QIcon(":/new/profix1/folder"));
QStandardItem *item03 = new QStandardItem("Foreign Language");
item03->setIcon(QIcon(":/new/profix1/folder"));
//子项
QStandardItem *item04 = new QStandardItem("Foreign Language A");
item04->setIcon(QIcon(":/new/profix1/file"));
item03->appendRow(item04);
standardModel->setItem(0, 0, item01);
standardModel->setItem(1, 0, item02);
standardModel->setItem(2, 0, item03);
treeView->setModel(standardModel);
}
qtButton::~qtButton()
{
}
//------------RadioButton------------------------
void qtButton::radioChange()
{
if (sender() == radioM)
{
label->setText("Man");
}
else if (sender() == radioW)
{
label->setText("Woman");
}
}
QString str;
void qtButton::checkChange()
{
if (sender() == checkBox01)
{
if (checkBox01->checkState() == Qt::Checked)
{
str += "Math";
}
else
{
str = str.replace(QString("Math"), QString(""));
}
}
else if (sender() == checkBox02)
{
if (checkBox02->checkState() == Qt::Checked)
{
str += " Chinese";
}
else
{
str = str.replace(QString("Chinese"), QString(""));
}
}
else if (sender() == checkBox03)
{
if (checkBox03->checkState() == Qt::Checked)
{
str += " Geometry";
}
else
{
str = str.replace(QString("Geometry"), QString(""));
}
}
label02->setText(str);
}
程序运行结果如下:
首先,给出qtButton.h的程序如下:
#ifndef QTBUTTON_H #define QTBUTTON_H #include <QtWidgets/QMainWindow> #include "ui_qtbutton.h" //------------------------------- #include<qradiobutton.h> #include<qlabel.h> #include<qcheckbox.h> #include<qlistview.h> #include<qstringlistmodel.h>//数据模型类 #include<qtreeview.h> #include<qstandarditemmodel.h> class qtButton : public QMainWindow { Q_OBJECT public: qtButton(QWidget *parent = 0); ~qtButton(); private: Ui::qtButtonClass ui; //--------------------------------- QRadioButton *radioM; QRadioButton *radioW; QLabel *label; QCheckBox *checkBox01; QCheckBox *checkBox02; QCheckBox *checkBox03; QLabel *label02; QListView *listView; QStringListModel *model; QTreeView *treeView; QStandardItemModel *standardModel; private slots: //--------------------------------------- void radioChange(); void checkChange(); }; #endif // QTBUTTON_H
其次,再给出qtButton.cpp中的程序:
#include "qtbutton.h"
qtButton::qtButton(QWidget *parent)
: QMainWindow(parent)
{
ui.setupUi(this);
//----------------RadioButton---------------
radioM = new QRadioButton(this);
radioW = new QRadioButton(this);
label = new QLabel(this);
radioM -> setGeometry(QRect(50, 50, 50, 50));
radioW->setGeometry(QRect(100, 50, 50, 50));
label->setGeometry(QRect(50, 100, 100, 25));
radioM->setText("Man");
radioW->setText("Woman");
//default values;
radioM->setChecked(true);
label->setText("Man");
connect(radioM, SIGNAL(clicked()), this, SLOT(radioChange()));
connect(radioW, SIGNAL(clicked()), this, SLOT(radioChange()));
//---------------CheckBox------------
checkBox01 = new QCheckBox(this);
checkBox02 = new QCheckBox(this);
checkBox03 = new QCheckBox(this);
label02 = new QLabel(this);
checkBox01->setGeometry(QRect(50, 150, 50, 50));
checkBox02->setGeometry(QRect(100, 150, 100, 50));
checkBox03->setGeometry(QRect(170, 150, 100, 50));
label02->setGeometry(QRect(50, 200, 200, 30));
label02->setText("initial");
checkBox01->setText("Math");
checkBox02->setText("Chinese");
checkBox03->setText("Geometry");
connect(checkBox01, SIGNAL(clicked(bool)), this, SLOT(checkChange()));
connect(checkBox02, SIGNAL(clicked(bool)), this, SLOT(checkChange()));
connect(checkBox03, SIGNAL(clicked(bool)), this, SLOT(checkChange()));
//----------ListView---------------
listView = new QListView(this);
listView->setGeometry(50, 250, 130, 100);
QStringList string;
string << "Math" << "Lauguage" << "Foreign Language" << "GeoMetry";
model = new QStringListModel(string);
listView->setModel(model);
//-----------QTreeView----------------------
treeView = new QTreeView(this);
treeView->setGeometry(QRect(200, 250, 300, 200));
standardModel = new QStandardItemModel(3, 2);
standardModel->setHeaderData(0, Qt::Horizontal, "The First List");
standardModel->setHeaderData(1, Qt::Horizontal, "The Second List");
QStandardItem *item01 = new QStandardItem("Math");
item01->setIcon(QIcon(":/new/profix1/folder"));
QStandardItem *item02 = new QStandardItem("Chinese");
item02->setIcon(QIcon(":/new/profix1/folder"));
QStandardItem *item03 = new QStandardItem("Foreign Language");
item03->setIcon(QIcon(":/new/profix1/folder"));
//子项
QStandardItem *item04 = new QStandardItem("Foreign Language A");
item04->setIcon(QIcon(":/new/profix1/file"));
item03->appendRow(item04);
standardModel->setItem(0, 0, item01);
standardModel->setItem(1, 0, item02);
standardModel->setItem(2, 0, item03);
treeView->setModel(standardModel);
}
qtButton::~qtButton()
{
}
//------------RadioButton------------------------
void qtButton::radioChange()
{
if (sender() == radioM)
{
label->setText("Man");
}
else if (sender() == radioW)
{
label->setText("Woman");
}
}
QString str;
void qtButton::checkChange()
{
if (sender() == checkBox01)
{
if (checkBox01->checkState() == Qt::Checked)
{
str += "Math";
}
else
{
str = str.replace(QString("Math"), QString(""));
}
}
else if (sender() == checkBox02)
{
if (checkBox02->checkState() == Qt::Checked)
{
str += " Chinese";
}
else
{
str = str.replace(QString("Chinese"), QString(""));
}
}
else if (sender() == checkBox03)
{
if (checkBox03->checkState() == Qt::Checked)
{
str += " Geometry";
}
else
{
str = str.replace(QString("Geometry"), QString(""));
}
}
label02->setText(str);
}
程序运行结果如下:
相关文章推荐
- release程序加载调用ffmpeg动态库崩溃
- Ubuntu在wps-office等qt5程序下不能切换中文fcitx输入法的问题
- 关于QT下资源使用和资源占用…
- qt 坐标变换
- Qt5模块简介
- qt5 connect问题
- qt 多语化
- qt deleterLater
- qt delete
- qt QClipBoard
- qt 共享内存 单例
- qt cef嵌入web
- QTableWidget 详细使用
- Qt学习五:控件二:下拉列表框、字体下拉列表框、QSpinBox控件、QScrollBar控件
- 一个qt的例子说明QWaitCondition的作用(条件变量)
- PyQt通过点击按钮选择文件并显示相对路径的例子
- Qt资料(一)
- Qt qss一些伪装态,以及margin与padding区别
- Qt双缓冲机制:实现一个简单的绘图工具(纯代码实现)
- 详细,Qt Creator快捷键大全,附快捷键配置方法