Qt学习六:控件三:单选按钮、复选框、列表控件、树控件

这是第三波控件描述;

首先,给出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);
}


程序运行结果如下:


  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值