C++ QT学习之路----控件应用

包含:QPushButton、QLabel、QLineEdit、QTextEdit、QPlainTextEdit、QComboBox、QFontComboBox、QSpinBox、QTimeEdit、QDateEdit、QScrollBar、QRadioButton、QCheckBox、QListView、QTreeView、QTableView、QHBoxLayout、QGridLayout、QGroupBox、QTabWidget、QMenu、QToolBar、任务栏托盘菜单等控件的使用。

cpp文件代码如下:

#include "QT_practice.h"
#include 
#include 
#include 
#include 

QT_practice::QT_practice(QWidget *parent)
    : QMainWindow(parent)
{
    ui.setupUi(this);

	//创建一个QLabel控件
	QLabel *label = new QLabel(this);
	//QLabel控件显示文字内容
	label->setText("Hello World!");
	//显示位置
	label->setGeometry(QRect(50, 50, 200, 25));

	//窗体标题
	QString str;
	str = str.fromLocal8Bit("Qt1.0 移动无边框窗体");
	/*
	//去掉标题栏
	this->setWindowFlags(Qt::FramelessWindowHint);
	//添加一个关闭按钮,实现关闭功能
	btClose = new QPushButton(this);
	btClose->setText("close");
	btClose->setGeometry(QRect(50, 100, 200, 25));
	//按钮点击事件
	connect(btClose, SIGNAL(clicked()), this, SLOT(close()));
	*/

	//去掉标题栏最大化、最小化按钮
	//this->setWindowFlags(Qt::WindowCloseButtonHint);
	this->setWindowTitle(str);
	//窗体ICO图片
	this->setWindowIcon(QIcon("qt.ico"));
	//窗体最大300*300
	this->setMaximumSize(1000, 1000);
	//窗体最小300*300
	this->setMinimumSize(1000, 1000);


	//默认窗体居中显示,如果想要更改,用move或setGeometry
	this->move(100,100);
	this->move(x() + 100, x() + 100);
	//设置背景透明
	//this->setAttribute(Qt::WA_TranslucentBackground, true);
	//this->setStyleSheet("background-image:url(:/new/prefixl/touming);background-repeat:no-repeat;");
	//背景灰色
	//this->setStyleSheet("background:grey");

	//实例QPushButton控件
	button = new QPushButton(this);
	//按钮显示位置
	str = str.fromLocal8Bit("打开子窗口");
	button->setText(str);
	button->setGeometry(QRect(100, 100, 100, 25));
	//点击事件
	connect(button, SIGNAL(clicked()), this, SLOT(showsubWindow()));

	//button改变文字
	str = str.fromLocal8Bit("按钮A");
	button = new QPushButton(str, this);
	//定义按钮X轴、Y轴,W宽、H高
	button->setGeometry(QRect(100, 150, 150, 25));
	//添加槽事件
	connect(button, SIGNAL(released()), this, SLOT(txtbutton()));

	//实例QLabel控件
	str = str.fromLocal8Bit("我是QLabel");
	label = new QLabel(str, this);
	//位置
	label->setGeometry(QRect(100, 200, 100, 25));
	label->setStyleSheet("font-size:20px;color:red;font-weight:bold;font-style:italic");

	//创建QLineEdit控件
	lineEdit = new QLineEdit(this);
	//位置大小
	lineEdit->setGeometry(QRect(100, 250, 200, 25));
	lineEdit->setStyleSheet("border:1px;border-style:solid;color:red;border-color:blue red;");
	//限制最长输入12位
	lineEdit->setMaxLength(12);
	//密码*号输入
	lineEdit->setEchoMode(QLineEdit::Password);

	//实例QTextEdit控件
	textEdit = new QTextEdit(this);
	//控件位置大小
	textEdit->setGeometry(QRect(100, 300, 200, 100));
	//内容
	str = str.fromLocal8Bit("我是第一行
我是第二行"); textEdit->setText(str); //实例 plainTextEdit = new QPlainTextEdit(this); //位置 plainTextEdit->setGeometry(QRect(100, 400, 200, 100)); //添加内容 str = str.fromLocal8Bit("第一行"); plainTextEdit->setPlainText(str); //实例QComboBox comboBox = new QComboBox(this); //控件显示位置大小 comboBox->setGeometry(QRect(100, 450, 120, 25)); //定义字符串列表 QStringList str_list; str_list << str.fromLocal8Bit("数学") << str.fromLocal8Bit("语文") << str.fromLocal8Bit("地理"); //将字符串列表绑定QComboBox控件 comboBox->addItems(str_list); //实例QFontComboBox fontComboBox = new QFontComboBox(this); //实例QPushButton fontbutton = new QPushButton(this); //实例QLabel fontlabel = new QLabel(this); fontlabel->setText(str.fromLocal8Bit("字体示例")); fontlabel->setGeometry(QRect(100, 500, 300, 25)); //按钮名 fontbutton->setText(str.fromLocal8Bit("按钮")); //位置 fontbutton->move(180, 50); //事件 connect(fontbutton, SIGNAL(released()), this, SLOT(fontButton())); //QFontComboBox控件位置 fontComboBox->setGeometry(QRect(50, 150, 120, 25)); //实例QSpinBox spinBox = new QSpinBox(this); //位置 spinBox->setGeometry(QRect(100, 600, 100, 25)); //值范围 spinBox->setRange(0, 2000); //初始值 spinBox->setValue(10); //后缀 spinBox->setSuffix(str.fromLocal8Bit("元")); //前缀 spinBox->setPrefix("$"); //实例 timeEdit = new QTimeEdit(this); //位置 timeEdit->setGeometry(QRect(200, 600, 120, 25)); //获取系统时间 QDateTime sysTime = QDateTime::currentDateTime(); //获取时分秒以“:”号拆分赋予list数组 QStringList list = sysTime.toString("hh:mm:ss").split(':'); //将时分秒绑定控件 timeEdit->setTime(QTime(list[0].toInt(), list[1].toInt(), list[2].toInt())); //实例 dateEdit = new QDateEdit(this); //位置 dateEdit->setGeometry(QRect(200, 750, 120, 25)); //获取系统时间 sysTime = QDateTime::currentDateTime(); //获取时分秒以“-”号拆分赋予list数组 list = sysTime.toString("yyyy-MM-dd").split('-'); //将年月日绑定控件 dateEdit->setDate(QDate(list[0].toInt(), list[1].toInt(), list[2].toInt())); //实例 scrollBar = new QScrollBar(this); spinBox1 = new QSpinBox(this); //横显/竖显 scrollBar->setOrientation(Qt::Horizontal); //位置 scrollBar->setGeometry(QRect(200, 650, 180, 20)); spinBox1->setGeometry(QRect(200, 700, 100, 25)); //控制条宽度 scrollBar->setPageStep(10); //scrollBar 事件 connect(scrollBar, SIGNAL(valueChanged(int)), spinBox1, SLOT(setValue(int))); //spinBox事件 connect(spinBox1, SIGNAL(valueChanged(int)), scrollBar, SLOT(setValue(int))); //初始值 scrollBar->setValue(50); //实例 radioM = new QRadioButton(this); radioW = new QRadioButton(this); radiolabel = new QLabel(this); //位置 radioM->setGeometry(QRect(400, 50, 50, 50)); radioW->setGeometry(QRect(450, 50, 50, 50)); radiolabel->setGeometry(QRect(400, 100, 100, 25)); radioM->setText("man"); radioW->setText("woman"); //默认选择 radioM->setChecked(true); radiolabel->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); //实例label控件显示结果 checklabel = new QLabel(this); //控件位置 checkBox01->setGeometry(QRect(400, 100, 50, 50)); checkBox02->setGeometry(QRect(450, 100, 50, 50)); checkBox03->setGeometry(QRect(500, 100, 50, 50)); checklabel->setGeometry(QRect(400, 150, 200, 30)); //控件值 checkBox01->setText(str.fromLocal8Bit("数学")); checkBox02->setText(str.fromLocal8Bit("语文")); checkBox03->setText(str.fromLocal8Bit("地理")); //checkbox控件点击事件 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(QRect(400, 200, 100, 100)); //StringList 数组 QStringList list_string; list_string << str.fromLocal8Bit("数学") << str.fromLocal8Bit("语文") << str.fromLocal8Bit("外语") << str.fromLocal8Bit("地理"); //添加数据 model = new QStringListModel(list_string); //将数据绑定listView控件 listView->setModel(model); //实例QTreeView 控件 treeView = new QTreeView(this); //位置 treeView->setGeometry(QRect(400, 300, 150, 200)); //实例数据类型4个节点,2列 treemodel = new QStandardItemModel(3, 2); //列名称 treemodel->setHeaderData(0, Qt::Horizontal, str.fromLocal8Bit("第一列")); treemodel->setHeaderData(1, Qt::Horizontal, str.fromLocal8Bit("第二列")); //定义节点 QStandardItem *item1 = new QStandardItem(str.fromLocal8Bit("数学")); item1->setIcon(QIcon("://new/prefix1/folder")); QStandardItem *item2 = new QStandardItem(str.fromLocal8Bit("语文")); item2->setIcon(QIcon(":/new/prefix1/folder")); QStandardItem *item3 = new QStandardItem(str.fromLocal8Bit("外语")); item3->setIcon(QIcon(":/new/prefix1/folder")); //外语子项 QStandardItem *item4 = new QStandardItem(str.fromLocal8Bit("外语A")); item4->setIcon(QIcon(":/new/prefix1/file")); item3->appendRow(item4); //将节点添加至QStandardItemModel treemodel->setItem(0, 0, item1); treemodel->setItem(1, 0, item2); treemodel->setItem(2, 0, item3); //将QStandardItemModel数据绑定QTreeView控件 treeView->setModel(treemodel); //实例 tableView = new QTableView(this); //位置 tableView->setGeometry(QRect(400, 500, 310, 200)); //实例数据模型 tablemodel = new QStandardItemModel(); //定义列 tablemodel->setHorizontalHeaderItem(0, new QStandardItem(str.fromLocal8Bit("数学"))); tablemodel->setHorizontalHeaderItem(1, new QStandardItem(str.fromLocal8Bit("语文"))); tablemodel->setHorizontalHeaderItem(2, new QStandardItem(str.fromLocal8Bit("外语"))); //行数据0行,0列 tablemodel->setItem(0, 0, new QStandardItem(str.fromLocal8Bit("数学A"))); tablemodel->setItem(0, 1, new QStandardItem(str.fromLocal8Bit("语文A"))); tablemodel->setItem(0, 2, new QStandardItem(str.fromLocal8Bit("外语A"))); tablemodel->setItem(1, 0, new QStandardItem(str.fromLocal8Bit("数学B"))); tablemodel->setItem(1, 1, new QStandardItem(str.fromLocal8Bit("语文B"))); tablemodel->setItem(1, 2, new QStandardItem(str.fromLocal8Bit("外语B"))); //将数据模型绑定控件 tableView->setModel(tablemodel); //创建横向布局 hboxLayout = new QHBoxLayout(); button1 = new QPushButton(str.fromLocal8Bit("按钮1")); button2 = new QPushButton(str.fromLocal8Bit("按钮2")); button3 = new QPushButton(str.fromLocal8Bit("按钮3")); //向布局中添加控件 hboxLayout->addWidget(button1); hboxLayout->addWidget(button2); hboxLayout->addWidget(button3); //间隔 hboxLayout->setSpacing(60); //实例QWidget widget = new QWidget(); //绑定布局 widget->setLayout(hboxLayout); //绑定界面 this->setCentralWidget(widget); //创建纵向布局 gridLayout = new QGridLayout(); gridbutton1 = new QPushButton(str.fromLocal8Bit("按钮1")); gridbutton2 = new QPushButton(str.fromLocal8Bit("按钮2")); gridbutton3 = new QPushButton(str.fromLocal8Bit("按钮3")); //向布局中添加控件 gridLayout->addWidget(gridbutton1, 0, 0, 1, 1); gridLayout->addWidget(gridbutton2, 0, 1, 1, 1); gridLayout->addWidget(gridbutton3, 1, 0, 1, 1); //实例QWidget gridwidget = new QWidget(); //绑定布局 gridwidget->setLayout(gridLayout); //绑定界面 this->setCentralWidget(gridwidget); //实例QGroupBox box = new QGroupBox(this); //位置、大小 box->setGeometry(QRect(400, 700, 340, 100)); //标题 box->setTitle(str.fromLocal8Bit("语音栏目")); //实例按钮 groupbutton = new QPushButton(); groupbutton->setText(str.fromLocal8Bit("按钮")); //实例布局 vbox = new QVBoxLayout; //将按钮加入布局 vbox->addWidget(groupbutton); //将布局加入QGroupBox控件 box->setLayout(vbox); //实例QTabWidget tabWidget = new QTabWidget(this); tabWidget->setGeometry(QRect(600, 30, 340, 140)); tabWidget->addTab(new tabA, str.fromLocal8Bit("A栏目")); tabWidget->addTab(new tabB, str.fromLocal8Bit("B栏目")); //填充菜单子节点 newAct = new QAction(QIcon(":/images/new"), tr("新建"), this); newAct->setShortcut(tr("Ctrl+N")); newAct->setStatusTip(tr("新建文件")); connect(newAct, SIGNAL(triggered()), this, SLOT(newFile())); cutAct = new QAction(QIcon(":/images/cut"), tr("剪切"), this); cutAct->setShortcut(tr("Ctrl+X")); cutAct->setStatusTip(tr("剪切内容")); copyAct = new QAction(QIcon(":/images/copy"), tr("复制"),this); copyAct->setShortcut(tr("Ctrl+C")); copyAct->setStatusTip(tr("复制内容")); pasteAct = new QAction(QIcon(":/images/paste"), tr("粘贴"), this); pasteAct->setShortcut(tr("Ctrl+V")); pasteAct->setStatusTip(tr("粘贴内容")); aboutQtAct = new QAction(tr("关于Qt"), this); aboutQtAct->setStatusTip(tr("关于Qt信息")); connect(aboutQtAct, SIGNAL(triggered()), qApp, SLOT(aboutQt())); //填充菜单 fileMenu = menuBar()->addMenu(tr("文件")); fileMenu->addAction(newAct); fileMenu->addSeparator(); editMenu = menuBar()->addMenu(tr("编辑")); editMenu->addAction(cutAct); editMenu->addAction(copyAct); editMenu->addAction(pasteAct); menuBar()->addSeparator(); helpMenu = menuBar()->addMenu(tr("帮助")); helpMenu->addAction(aboutQtAct); //toolBar工具条 fileToolBar = addToolBar(tr("新建")); fileToolBar->addAction(newAct); editToolBar = addToolBar(tr("修改")); editToolBar->addAction(cutAct); editToolBar->addAction(copyAct); editToolBar->addAction(pasteAct); //菜单 createMenu(); //判断系统是否支持托盘图标 if (!QSystemTrayIcon::isSystemTrayAvailable()) { return; } //实例QSystemTrayIcon myTrayIcon = new QSystemTrayIcon(this); //设置图标 myTrayIcon->setIcon(QIcon(":/new/prefixl/ico")); //鼠标放托盘图标上提示信息 myTrayIcon->setToolTip("Qt托盘图标功能"); //设置消息 myTrayIcon->showMessage("托盘", "托盘管理", QSystemTrayIcon::Information, 10000); //托盘菜单 myTrayIcon->setContextMenu(myMenu); //显示 myTrayIcon->show(); } //绘制菜单 void QT_practice::createMenu() { restoreWinAction = new QAction("恢复(&R)", this); quitAction = new QAction("退出(&Q)", this); //恢复 connect(restoreWinAction, SIGNAL(triggered()), this, SLOT(showNormal())); //退出 connect(quitAction, SIGNAL(triggered()), qApp, SLOT(quit())); myMenu = new QMenu((QWidget*)QApplication::desktop()); //添加菜单 myMenu->addAction(restoreWinAction); //分隔符 myMenu->addSeparator(); myMenu->addAction(quitAction); } //恢复 void QT_practice::showNormal() { this->show(); } //点击最小化按钮隐藏界面 void QWidget::changeEvent(QEvent *e) { if ((e->type() == QEvent::WindowStateChange) && this->isMinimized()) { //QTimer::singleShot(100,this,SLOT(hide())); this->hide(); } } //子菜单事件 void QT_practice::newFile() { QMessageBox::warning(this, tr("Warning"), tr("创建新文件?"), QMessageBox::Yes | QMessageBox::Default, QMessageBox::No); } tabA::tabA(QWidget *parent) :QWidget(parent) { QString str; QPushButton *buttonA = new QPushButton(this); buttonA->setText(str.fromLocal8Bit("页面A")); } tabB::tabB(QWidget *parent) :QWidget(parent) { QString str; QPushButton *buttonB = new QPushButton(this); buttonB->setText(str.fromLocal8Bit("页面B")); } void QT_practice::showsubWindow() { //显示subWindow窗体 sw.show(); } void QT_practice::txtbutton() { QString str; str = str.fromLocal8Bit("按钮B"); button->setText(str); } //方法 void QT_practice::fontButton() { QString str; fontlabel->setText(str.fromLocal8Bit("选择字体:") + fontComboBox->currentText()); } //radio点击方法 void QT_practice::radioChange() { if (sender() == radioM) { radiolabel->setText("man"); } else if (sender() == radioW) { radiolabel->setText("woman"); } } QString check_str; void QT_practice::checkChange() { QString str; if (sender() == checkBox01) { //判断是否被选中 if (checkBox01->checkState() == Qt::Checked) { check_str += str.fromLocal8Bit("数学"); } else { check_str = check_str.replace(str.fromLocal8Bit("数学"), QString("")); } } else if (sender() == checkBox02) { if (checkBox02->checkState() == Qt::Checked) { check_str += str.fromLocal8Bit("语文"); } else { check_str = check_str.replace(str.fromLocal8Bit("语文"), QString("")); } } else if (sender() == checkBox03) { if (checkBox03->checkState() == Qt::Checked) { check_str += str.fromLocal8Bit("地理"); } else { check_str = check_str.replace(str.fromLocal8Bit("地理"), QString("")); } } //绑定值 checklabel->setText(check_str); } /* void QT_practice::mousePressEvent(QMouseEvent *e) { last = e->globalPos(); } void QT_practice::mouseMoveEvent(QMouseEvent *e) { int dx = e->globalX() - last.x(); int dy = e->globalY() - last.y(); last = e->globalPos(); move(this->pos().x() + dx, this->pos().y() + dy);//这里(x(),y())是父窗体左上角在屏幕中的位置 } void QT_practice::mouseReleaseEvent(QMouseEvent *e) { int dx = e->globalX() - last.x(); int dy = e->globalY() - last.y(); move(x() + dx, y() + dy); } */

头文件代码如下:

#pragma once

#include 
#include "ui_QT_practice.h"

#include 	//鼠标类头文件
#include 	//引用按钮类头文件
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 	//QListView类
#include 	//数据模型类
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 	//任务栏类
#include "subwindow.h"	//引用子窗体

class QT_practice : public QMainWindow
{
    Q_OBJECT

public:
    QT_practice(QWidget *parent = Q_NULLPTR);

private:
    Ui::QT_practiceClass ui;
	QPushButton *button;
	QPushButton *changetxtButton;
	subWindow sw;

	//声明QLabel
	QLabel *label;
	//声明QLineEdit
	QLineEdit *lineEdit;
	//声明QTextEdit
	QTextEdit *textEdit;
	QPlainTextEdit *plainTextEdit;
	QComboBox *comboBox;
	
	//QFontComboBox字体下拉列表框
	QFontComboBox *fontComboBox;
	QPushButton *fontbutton;
	QLabel *fontlabel;
	QSpinBox *spinBox;

	QTimeEdit *timeEdit;
	QDateEdit *dateEdit;
	QScrollBar *scrollBar;
	QSpinBox *spinBox1;
	QRadioButton *radioM;
	QRadioButton *radioW;
	QLabel *radiolabel;
	QCheckBox *checkBox01;
	QCheckBox *checkBox02;
	QCheckBox *checkBox03;
	QLabel *checklabel;
	QListView *listView;
	QStringListModel *model;
	QTreeView *treeView;
	QStandardItemModel *treemodel;
	QTableView *tableView;
	QStandardItemModel *tablemodel;
	QHBoxLayout *hboxLayout;
	QPushButton *button1;
	QPushButton *button2;
	QPushButton *button3;
	QWidget *widget;
	QGridLayout *gridLayout;
	QPushButton *gridbutton1;
	QPushButton *gridbutton2;
	QPushButton *gridbutton3;
	QWidget *gridwidget;
	QGroupBox *box;
	QPushButton *groupbutton;
	QVBoxLayout *vbox;
	QTabWidget *tabWidget;
	QMenu *fileMenu, *editMenu, *helpMenu;
	QToolBar *fileToolBar, *editToolBar;
	QAction *newAct;
	QAction *cutAct;
	QAction *copyAct;
	QAction *pasteAct;
	QAction *aboutQtAct;
	QSystemTrayIcon *myTrayIcon;
	QMenu *myMenu;
	QAction *restoreWinAction;
	QAction *quitAction;
	void createMenu();
private slots:
	void showsubWindow();
	void txtbutton();
	void fontButton();
	void radioChange();
	void checkChange();
	void newFile();
	void showNormal();
public:
	
	/*
protected:
	//鼠标按下
	void mousePressEvent(QMouseEvent *e);
	//鼠标移动
	void mouseMoveEvent(QMouseEvent *e);
	//鼠标释放
	void mouseReleaseEvent(QMouseEvent *e);

private:
	QPushButton *btClose;
	QPoint last;*/
};
//tabA类
class tabA :public QWidget
{
	Q_OBJECT
public:
	tabA(QWidget *parent = 0);
};
//tabB类
class tabB :public QWidget
{
	Q_OBJECT
public:
	tabB(QWidget *parent = 0);
};

写得比较杂乱,仅供以后查询控件使用方法用。

另外,存在一个问题,就是界面总是出现中文乱码,只有用QString.FromLocal8Bit()转换一下,才不会出现乱码,不知道VS工具里哪里可以设置一下,一劳永逸,不用每次都去转换。

你可能感兴趣的:(C++ QT学习之路----控件应用)