1. QMainWindow
1.1菜单栏 QMenuBar * bar = MenuBar() 只能最多有一个
1.1.1把这个栏添加到 窗口中 setMenuBar
1.1.2添加菜单 addMenu(文件)
1.1.3文件里添加菜单项 addAction(新建) 返回QAction
1.1.4添加分隔符 addSeparator
1.2工具栏 可以有多个
1.2.1tBar = new QToolBar
1.2.2addToolBar( 默认停靠位置,tBar)
1.2.3设置 停靠、浮动、移动
1.2.4添加菜单项
1.3状态栏 statusBar 只能一个
1.3.1左侧添加
1.3.2右侧添加
1.4铆接部件 可以多个
1.4.1QDockWidget
1.4.2addDockWidget(默认位置,。。)
1.4.3设置后期的停靠位置
1.5核心部件 只能一个
1.5.1setCentralWidget
01_QMainWindow.pro
#-------------------------------------------------
#
# Project created by QtCreator 2017-05-06T09:20:20
#
#-------------------------------------------------
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = 01_QMainWindow
TEMPLATE = app
SOURCES += main.cpp\
mainwindow.cpp
HEADERS += mainwindow.h
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = 0);
~MainWindow();
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include <QMenuBar>
#include <QToolBar>
#include <QLabel>
#include <QStatusBar>
#include <QDockWidget>
#include <QTextEdit>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
resize(600,400);
// 包含菜单栏 只能有一个
QMenuBar * bar = menuBar();
//将菜单栏放入到窗口中
this->setMenuBar(bar);
//创建文件菜单
QMenu * fileMenu = bar->addMenu("文件");
QMenu * editMenu = bar->addMenu("编辑");
//添加菜单项
QAction * newAction = fileMenu->addAction("新建");
// 添加分割线
fileMenu->addSeparator();
QAction * openAction = fileMenu->addAction("打开");
//工具栏 可以有多个
QToolBar * toolBar = new QToolBar(this);
addToolBar(Qt::LeftToolBarArea,toolBar); //默认停靠范围
//只允许左右侧停靠
toolBar->setAllowedAreas(Qt::LeftToolBarArea | Qt::RightToolBarArea );
//设置浮动
toolBar->setFloatable(false);
//设置移动 (总开关)
toolBar->setMovable(false);
//工具栏添加菜单项
toolBar->addAction(newAction);
//添加分割线
toolBar->addSeparator();
toolBar->addAction(openAction);
//状态栏 只能有一个
QStatusBar * stBar = statusBar();
setStatusBar(stBar);
QLabel * label = new QLabel("提示信息",this);
stBar->addWidget(label); //添加提示信息到左侧
QLabel * label2 = new QLabel("右侧提示信息",this);
stBar->addPermanentWidget(label2);
//铆接部件 浮动窗口 可以有多个
QDockWidget * dock = new QDockWidget;
//添加铆接部件到 窗口中
addDockWidget(Qt::BottomDockWidgetArea,dock);
//设置停靠范围
dock->setAllowedAreas(Qt::TopDockWidgetArea | Qt::BottomDockWidgetArea);
//核心部件 只能一个
QTextEdit * edit = new QTextEdit; //文本编辑框
setCentralWidget(edit);
}
MainWindow::~MainWindow()
{
}
main.cpp
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
2. 添加资源文件
2.1首先将资源导入到项目中
2.2右键项目 - 添加新文件 – Qt - Qt Resource File
2.3给资源起名称 res
2.4Qt会生成res.qrc 文件
2.5右键res.qrc open in Editor
2.6添加前缀
2.7添加文件
2.8使用 “ : + 前缀名 + 文件名 ”
2.9可以设置别名,但是不建议,因为设置别名,原来的方式就不可用了
02_Source.pro
#-------------------------------------------------
#
# Project created by QtCreator 2017-05-06T10:27:09
#
#-------------------------------------------------
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = 02_Source
TEMPLATE = app
SOURCES += main.cpp\
mainwindow.cpp
HEADERS += mainwindow.h
FORMS += mainwindow.ui
RESOURCES += \
res.qrc
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
//通过ui寻找控件
//ui->actionNew->setIcon(QIcon("E:/Image/Luffy.png"));
//添加资源文件 到项目中
//使用资源文件 " : + 前缀名 + 文件名 "
ui->actionNew->setIcon(QIcon(":/Image/Luffy.png"));
ui->actionOpen->setIcon(QIcon(":/Image/OnePiece.png"));
}
MainWindow::~MainWindow()
{
delete ui;
}
mainwindow.ui
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>300</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralWidget"/>
<widget class="QMenuBar" name="menuBar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>23</height>
</rect>
</property>
<widget class="QMenu" name="menu">
<property name="title">
<string>文件</string>
</property>
<addaction name="actionNew"/>
<addaction name="separator"/>
<addaction name="actionOpen"/>
</widget>
<widget class="QMenu" name="menu_2">
<property name="title">
<string>编辑</string>
</property>
</widget>
<addaction name="menu"/>
<addaction name="menu_2"/>
</widget>
<widget class="QToolBar" name="mainToolBar">
<property name="allowedAreas">
<set>Qt::BottomToolBarArea|Qt::TopToolBarArea</set>
</property>
<attribute name="toolBarArea">
<enum>TopToolBarArea</enum>
</attribute>
<attribute name="toolBarBreak">
<bool>false</bool>
</attribute>
<addaction name="actionNew"/>
<addaction name="separator"/>
<addaction name="actionOpen"/>
</widget>
<widget class="QStatusBar" name="statusBar"/>
<action name="actionNew">
<property name="text">
<string>新建</string>
</property>
</action>
<action name="actionOpen">
<property name="text">
<string>打开</string>
</property>
</action>
</widget>
<layoutdefault spacing="6" margin="11"/>
<resources/>
<connections/>
</ui>
main.cpp
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
res.qrc
<RCC>
<qresource prefix="/">
<file>Image/butterfly.png</file>
<file>Image/butterfly1.png</file>
<file>Image/down.png</file>
<file>Image/Frame.jpg</file>
<file>Image/Luffy.png</file>
<file>Image/LuffyQ.png</file>
<file>Image/mario.gif</file>
<file>Image/OnePiece.png</file>
<file>Image/Sunny.jpg</file>
<file>Image/sunny.png</file>
<file>Image/up.png</file>
</qresource>
</RCC>
3. 对话框
3.1模态对话框
3.1.1Dialog dlg
3.1.2dlg.exec() 阻塞功能
3.2非模态对话框
3.2.1Dialog dlg
3.2.2dlg.show() 会一闪而过 所以创建在堆上
3.2.3new dlg dlg->show()
3.2.4设置属性 55号 setAttribute(Qt::WA_DeleteOnClose)
03_QDialog.pro
#-------------------------------------------------
#
# Project created by QtCreator 2017-05-06T11:11:42
#
#-------------------------------------------------
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = 03_QDialog
TEMPLATE = app
SOURCES += main.cpp\
mainwindow.cpp
HEADERS += mainwindow.h
FORMS += mainwindow.ui
CONFIG += c++11
mainwindow.ui
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>300</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralWidget"/>
<widget class="QMenuBar" name="menuBar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>23</height>
</rect>
</property>
<widget class="QMenu" name="menu">
<property name="title">
<string>文件</string>
</property>
<addaction name="actionNew"/>
<addaction name="actionOpen"/>
</widget>
<addaction name="menu"/>
</widget>
<widget class="QToolBar" name="mainToolBar">
<attribute name="toolBarArea">
<enum>TopToolBarArea</enum>
</attribute>
<attribute name="toolBarBreak">
<bool>false</bool>
</attribute>
<addaction name="actionNew"/>
<addaction name="actionOpen"/>
</widget>
<widget class="QStatusBar" name="statusBar"/>
<action name="actionNew">
<property name="text">
<string>新建</string>
</property>
</action>
<action name="actionOpen">
<property name="text">
<string>打开</string>
</property>
</action>
</widget>
<layoutdefault spacing="6" margin="11"/>
<resources/>
<connections/>
</ui>
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
#include <QDialog>
#include <QMessageBox>
#include <QColorDialog>
#include <QFileDialog>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
//点击新建菜单项 弹出对话框
connect(ui->actionNew,&QAction::triggered,this,[=](){
//对话框 有两种
// 模态对话框 (不可以对其他窗口进行操作) 非模态对话框 (可以对其他窗口操作)
// QDialog dlg(this);
// dlg.resize(200,100);
// dlg.exec(); //阻塞
// qDebug() << "弹出对话框!";
//非模态对话框创建
//QDialog dlg2(this); 创建到栈上 一闪而过
// QDialog * dlg2 = new QDialog(this);
// dlg2->resize(200,100);
// dlg2->show();
// //需要设置属性 dlg2 55号
// dlg2->setAttribute(Qt::WA_DeleteOnClose);
//使用标准对话框 QMessageBox
//错误对话框
//QMessageBox::critical(this,"错误!","critical");
//信息对话框
//QMessageBox::information(this,"信息","info");
//询问对话框
// 参数1 、父亲 2、标题 3、提示内容 4 按键类型 5 关联回车按键
// if(QMessageBox::Save == QMessageBox::question(this,"问题","question",QMessageBox::Save | QMessageBox::Cancel,QMessageBox::Cancel))
// {
// qDebug() << "点击的是保存";
// }
// else
// {
// qDebug() << "点击的是取消";
// }
//警告对话框
// QMessageBox::warning(this,"警告!","warning");
//选择颜色对话框
// QColor color = QColorDialog::getColor(QColor(255,0,0));
// qDebug() << color.red() << color.green() << color.blue();
//文件对话框
// QString path = QFileDialog::getOpenFileName(this,"打开文件","C:\\Users\\zhangtao\\Desktop","(*.txt *.png)");
// qDebug() << path;
});
}
MainWindow::~MainWindow()
{
delete ui;
}
main.cpp
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
4. QMessageBox
4.1错误、信息、警告、问题
4.2问题 5个参数
4.2.11 父亲 2 标题 3 显示内容 4 按键类型 5 默认关联回车按键
5. QFileDialog
5.1getOpenFileName(父亲,标题,默认路径,过滤文件格式)
5.2返回值是文件路径
6. QColorDialog
6.1getColor( 默认色 QColor(r,g,b))
6.2返回值 QColor
7. 界面布局
7.1登陆窗口
7.2水平布局 和 垂直布局
7.3默认widget和控件之间有9像素的间隙
7.4灵活运用弹簧
04_Layout.pro
#-------------------------------------------------
#
# Project created by QtCreator 2017-05-06T14:33:41
#
#-------------------------------------------------
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = 04_Layout
TEMPLATE = app
SOURCES += main.cpp\
widget.cpp
HEADERS += widget.h
FORMS += widget.ui
widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
namespace Ui {
class Widget;
}
class Widget : public QWidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = 0);
~Widget();
private:
Ui::Widget *ui;
};
#endif // WIDGET_H
widget.cpp
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
}
Widget::~Widget()
{
delete ui;
}
main.cpp
#include "widget.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
return a.exec();
}
widget.ui
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Widget</class>
<widget class="QWidget" name="Widget">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>320</width>
<height>230</height>
</rect>
</property>
<property name="minimumSize">
<size>
<width>320</width>
<height>230</height>
</size>
</property>
<property name="maximumSize">
<size>
<width>320</width>
<height>230</height>
</size>
</property>
<property name="windowTitle">
<string>登陆窗口</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout_2">
<item>
<widget class="QWidget" name="widget" native="true">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<layout class="QGridLayout" name="gridLayout">
<property name="leftMargin">
<number>0</number>
</property>
<property name="topMargin">
<number>0</number>
</property>
<property name="rightMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<number>0</number>
</property>
<item row="0" column="3">
<spacer name="horizontalSpacer_5">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item row="2" column="1">
<widget class="QLabel" name="label_2">
<property name="text">
<string>密码:</string>
</property>
</widget>
</item>
<item row="0" column="2">
<widget class="QLineEdit" name="lineEdit"/>
</item>
<item row="0" column="1">
<widget class="QLabel" name="label">
<property name="text">
<string>用户名:</string>
</property>
</widget>
</item>
<item row="2" column="2">
<widget class="QLineEdit" name="lineEdit_2"/>
</item>
<item row="0" column="0">
<spacer name="horizontalSpacer_4">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item row="1" column="1">
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeType">
<enum>QSizePolicy::Fixed</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
</layout>
</widget>
</item>
<item>
<widget class="QWidget" name="widget_3" native="true">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<layout class="QHBoxLayout" name="horizontalLayout_5">
<property name="leftMargin">
<number>0</number>
</property>
<property name="topMargin">
<number>0</number>
</property>
<property name="rightMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<number>0</number>
</property>
<item>
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QPushButton" name="pushButton">
<property name="text">
<string>登陆</string>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer_3">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeType">
<enum>QSizePolicy::Fixed</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>30</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QPushButton" name="pushButton_2">
<property name="text">
<string>退出</string>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer_2">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
</layout>
</widget>
</item>
</layout>
</widget>
<layoutdefault spacing="6" margin="11"/>
<resources/>
<connections/>
</ui>
8. 控件
8.1按钮组
8.1.1QPushButton 可以加Icon
8.1.2QToolButton 主要显示Icon,想显示图片和文字 ,修改属性toolButtonStyle
8.1.2.1自带效果 透明效果 autoRaise
8.1.3QRadioButton
8.1.3.1setChecked 设置默认选中
8.1.3.2监听clicked信号
8.1.4QCheckButton
8.1.4.1statusChanged 状态改变 0 未选中 1 半选 2 全选
8.2ListWidget
8.2.1QListWidgetItem * item = new … (“诗词”)
8.2.2ui->listWidget->addItem(item);
8.2.3设置对齐方式 item->setTextAlignment(Qt::AlignHCenter);
8.2.4addItems( QStringlist)
8.3TreeWidget
8.3.1设置头
8.3.2ui->treeWidget->setHeaderLabels(QStringList() << “英雄”<<“英雄介绍”);
8.3.2.1设置具体内容
8.3.3创建顶层的item
8.3.3.1QTreeWidgetItem * liItem = new …
8.3.3.2ui->treeWidget->addTopLevelItem(liItem);
8.3.4设置子节点
8.3.4.1addChild()
8.4TableWidget
8.4.1设置列数
8.4.2设置头 姓名 性别 年龄
8.4.3设置行数
8.4.4设置正文 setItem(row,col,QTableWidgetItem)
8.4.5添加赵云
8.4.5.1判断是否为空
8.4.5.2为空 添加 insertRow
8.4.6删除赵云
8.4.6.1判断是否为空
8.4.6.2先确定赵云所在的row
8.4.6.3removeRow
8.5其他常用控件
8.5.1stackedwidget 栈容器
8.5.1.1设置所以 setCurrentIndex
8.5.2下拉框
8.5.2.1addItem添加项目
8.5.3利用QLabel显示图片
8.5.3.1setPixmap(QPixmap(“文件路径”))
8.5.4利用QLabel显示gif图片
8.5.4.1 QMovie * movie = new QMovie(":/Image/mario.gif");
8.5.4.2 ui->label_gif->setMovie(movie);
8.5.4.3 movie->start();
05_Control
05_Control.pro
#-------------------------------------------------
#
# Project created by QtCreator 2017-05-06T15:06:47
#
#-------------------------------------------------
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = 05_Control
TEMPLATE = app
SOURCES += main.cpp\
widget.cpp
HEADERS += widget.h
FORMS += widget.ui
RESOURCES += \
res.qrc
widget.ui
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Widget</class>
<widget class="QWidget" name="Widget">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>579</width>
<height>463</height>
</rect>
</property>
<property name="windowTitle">
<string>Widget</string>
</property>
<widget class="QPushButton" name="pushButton">
<property name="geometry">
<rect>
<x>10</x>
<y>10</y>
<width>175</width>
<height>50</height>
</rect>
</property>
<property name="text">
<string>登陆</string>
</property>
<property name="icon">
<iconset resource="res.qrc">
<normaloff>:/Image/Sunny.jpg</normaloff>:/Image/Sunny.jpg</iconset>
</property>
<property name="iconSize">
<size>
<width>32</width>
<height>32</height>
</size>
</property>
</widget>
<widget class="QToolButton" name="toolButton">
<property name="geometry">
<rect>
<x>20</x>
<y>70</y>
<width>121</width>
<height>51</height>
</rect>
</property>
<property name="text">
<string>海贼旗</string>
</property>
<property name="icon">
<iconset resource="res.qrc">
<normaloff>:/Image/OnePiece.png</normaloff>:/Image/OnePiece.png</iconset>
</property>
<property name="iconSize">
<size>
<width>32</width>
<height>32</height>
</size>
</property>
<property name="toolButtonStyle">
<enum>Qt::ToolButtonTextBesideIcon</enum>
</property>
<property name="autoRaise">
<bool>true</bool>
</property>
</widget>
<widget class="QGroupBox" name="groupBox">
<property name="geometry">
<rect>
<x>40</x>
<y>130</y>
<width>55</width>
<height>70</height>
</rect>
</property>
<property name="title">
<string>性别</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QRadioButton" name="rBtnMan">
<property name="text">
<string>男</string>
</property>
</widget>
</item>
<item>
<widget class="QRadioButton" name="rBtnWoman">
<property name="text">
<string>女</string>
</property>
</widget>
</item>
</layout>
</widget>
<widget class="QGroupBox" name="groupBox_2">
<property name="geometry">
<rect>
<x>100</x>
<y>130</y>
<width>67</width>
<height>70</height>
</rect>
</property>
<property name="title">
<string>婚否</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout_2">
<item>
<widget class="QRadioButton" name="radioButton_3">
<property name="text">
<string>已婚</string>
</property>
</widget>
</item>
<item>
<widget class="QRadioButton" name="radioButton_4">
<property name="text">
<string>未婚</string>
</property>
</widget>
</item>
</layout>
</widget>
<widget class="QGroupBox" name="groupBox_3">
<property name="geometry">
<rect>
<x>40</x>
<y>220</y>
<width>91</width>
<height>114</height>
</rect>
</property>
<property name="title">
<string>调查问卷</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout_3">
<item>
<widget class="QCheckBox" name="checkBox">
<property name="text">
<string>服务好</string>
</property>
<property name="tristate">
<bool>true</bool>
</property>
</widget>
</item>
<item>
<widget class="QCheckBox" name="checkBox_2">
<property name="text">
<string>口味好</string>
</property>
</widget>
</item>
<item>
<widget class="QCheckBox" name="checkBox_3">
<property name="text">
<string>环境好</string>
</property>
</widget>
</item>
<item>
<widget class="QCheckBox" name="checkBox_4">
<property name="text">
<string>老板娘好</string>
</property>
</widget>
</item>
</layout>
</widget>
<widget class="QListWidget" name="listWidget">
<property name="geometry">
<rect>
<x>200</x>
<y>20</y>
<width>361</width>
<height>331</height>
</rect>
</property>
</widget>
</widget>
<layoutdefault spacing="6" margin="11"/>
<resources>
<include location="res.qrc"/>
</resources>
<connections/>
</ui>
widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
namespace Ui {
class Widget;
}
class Widget : public QWidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = 0);
~Widget();
private:
Ui::Widget *ui;
};
#endif // WIDGET_H
widget.cpp
#include "widget.h"
#include "ui_widget.h"
#include <QDebug>
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
//单选按钮 默认选中 男
ui->rBtnMan->setChecked(true);
//点击女的 就打印选中了
connect(ui->rBtnWoman, &QRadioButton::clicked,[=](){
qDebug() << "选中女的了!";
});
//多选框 选中后打印内容
//选中2 未选中 0 tristate 1状态
connect(ui->checkBox,&QCheckBox::stateChanged,[=](int state){
qDebug() << state ;
});
//利用listWidget 写诗
// QListWidgetItem * item = new QListWidgetItem("锄禾日当午");
// //设置对齐方式
// item->setTextAlignment(Qt::AlignHCenter);
// ui->listWidget->addItem(item);
//QStringList === QList<QString>
QStringList list;
list << "锄禾日当午"<< "汗滴禾下土"<< "谁知盘中餐"<< "粒粒皆辛苦";
//QStringList()<<"锄禾日当午"<< "汗滴禾下土"<< "谁知盘中餐"<< "粒粒皆辛苦" 匿名对象也可以直接使用
ui->listWidget->addItems(list);
}
Widget::~Widget()
{
delete ui;
}
main.cpp
#include "widget.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
return a.exec();
}
res.qrc
<RCC>
<qresource prefix="/">
<file>Image/butterfly.png</file>
<file>Image/butterfly1.png</file>
<file>Image/down.png</file>
<file>Image/Frame.jpg</file>
<file>Image/Luffy.png</file>
<file>Image/LuffyQ.png</file>
<file>Image/mario.gif</file>
<file>Image/OnePiece.png</file>
<file>Image/Sunny.jpg</file>
<file>Image/sunny.png</file>
<file>Image/up.png</file>
</qresource>
</RCC>
06_Control_TreeWidget
06_Control_TreeWidget.pro
#-------------------------------------------------
#
# Project created by QtCreator 2017-05-06T15:49:20
#
#-------------------------------------------------
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = 06_Control_TreeWidget
TEMPLATE = app
SOURCES += main.cpp\
widget.cpp
HEADERS += widget.h
FORMS += widget.ui
widget.ui
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Widget</class>
<widget class="QWidget" name="Widget">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>504</width>
<height>341</height>
</rect>
</property>
<property name="windowTitle">
<string>Widget</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QTreeWidget" name="treeWidget">
<column>
<property name="text">
<string notr="true">1</string>
</property>
</column>
</widget>
</item>
</layout>
</widget>
<layoutdefault spacing="6" margin="11"/>
<resources/>
<connections/>
</ui>
widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
namespace Ui {
class Widget;
}
class Widget : public QWidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = 0);
~Widget();
private:
Ui::Widget *ui;
};
#endif // WIDGET_H
widget.cpp
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
//treeWidget控件使用
//添加头
ui->treeWidget->setHeaderLabels(QStringList()<< "英雄姓名"<< "英雄介绍");
// 添加项目
QTreeWidgetItem * liItem = new QTreeWidgetItem(QStringList() << "力量");
QTreeWidgetItem * minItem = new QTreeWidgetItem(QStringList() << "敏捷");
QTreeWidgetItem * zhiItem = new QTreeWidgetItem(QStringList() << "智力");
//添加顶层项目
ui->treeWidget->addTopLevelItem(liItem);
ui->treeWidget->addTopLevelItem(minItem);
ui->treeWidget->addTopLevelItem(zhiItem);
QStringList heroL1;
QStringList heroL2;
QStringList heroM1;
QStringList heroM2;
QStringList heroZ1;
QStringList heroZ2;
heroL1 << "刚被猪" << "前排坦克,能在吸收伤害的同时造成可观的范围输出";
heroL2 << "船长" << "前排坦克,能肉能输出能控场的全能英雄";
heroM1 << "月骑" << "中排物理输出,可以使用分裂利刃攻击多个目标";
heroM2 << "小鱼人" << "前排战士,擅长偷取敌人的属性来增强自身战力";
heroZ1 << "死灵法师" << "前排法师坦克,魔法抗性较高,拥有治疗技能";
heroZ2 << "巫医" << "后排辅助法师,可以使用奇特的巫术诅咒敌人与治疗队友";
//追加子项目 子项也是QTreeWidgetItem
QTreeWidgetItem * li1 =new QTreeWidgetItem(heroL1);
liItem->addChild(li1);
QTreeWidgetItem * li2 =new QTreeWidgetItem(heroL2);
liItem->addChild(li2);
QTreeWidgetItem * Min1 =new QTreeWidgetItem(heroM1);
minItem->addChild(Min1);
QTreeWidgetItem * Min2 =new QTreeWidgetItem(heroM2);
minItem->addChild(Min2);
QTreeWidgetItem * Zhi1 =new QTreeWidgetItem(heroZ1);
zhiItem->addChild(Zhi1);
QTreeWidgetItem * Zhi2 =new QTreeWidgetItem(heroZ2);
zhiItem->addChild(Zhi2);
}
Widget::~Widget()
{
delete ui;
}
main.cpp
#include "widget.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
return a.exec();
}
07_Control_TableWidget
07_Control_TableWidget.pro
#-------------------------------------------------
#
# Project created by QtCreator 2017-05-06T16:31:01
#
#-------------------------------------------------
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = 07_Control_TableWidget
TEMPLATE = app
SOURCES += main.cpp\
widget.cpp
HEADERS += widget.h
FORMS += widget.ui
widget.ui
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Widget</class>
<widget class="QWidget" name="Widget">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>564</width>
<height>461</height>
</rect>
</property>
<property name="windowTitle">
<string>Widget</string>
</property>
<widget class="QTableWidget" name="tableWidget">
<property name="geometry">
<rect>
<x>10</x>
<y>30</y>
<width>381</width>
<height>321</height>
</rect>
</property>
</widget>
<widget class="QPushButton" name="addBtn">
<property name="geometry">
<rect>
<x>420</x>
<y>100</y>
<width>75</width>
<height>23</height>
</rect>
</property>
<property name="text">
<string>添加赵云</string>
</property>
</widget>
<widget class="QPushButton" name="delBtn">
<property name="geometry">
<rect>
<x>420</x>
<y>190</y>
<width>75</width>
<height>23</height>
</rect>
</property>
<property name="text">
<string>删除赵云</string>
</property>
</widget>
</widget>
<layoutdefault spacing="6" margin="11"/>
<resources/>
<connections/>
</ui>
widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
namespace Ui {
class Widget;
}
class Widget : public QWidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = 0);
~Widget();
private:
Ui::Widget *ui;
};
#endif // WIDGET_H
widget.cpp
#include "widget.h"
#include "ui_widget.h"
#include <QMessageBox>
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
//QTableWidget控件使用
//告诉控件 一共有多少列
QStringList list;
list << "姓名"<< "性别"<< "年龄";
ui->tableWidget->setColumnCount(list.size());
//设置水平头
ui->tableWidget->setHorizontalHeaderLabels(list);
//设置行数
ui->tableWidget->setRowCount(5);
//设置正文
//ui->tableWidget->setItem(0,0,new QTableWidgetItem("亚瑟"));
//准备数据
QStringList nameList;
nameList << "亚瑟"<< "妲己"<< "安琪拉"<< "东皇太一"<< "李白";
QList<QString> sexList;
sexList << "男" << "女"<< "女"<< "男"<< "男";
for(int i = 0 ; i < 5;i++)
{
int col = 0;
ui->tableWidget->setItem(i,col++,new QTableWidgetItem(nameList[i]));
//添加性别
ui->tableWidget->setItem(i,col++,new QTableWidgetItem(sexList.at(i)));
//添加年龄
//int 转 QString number
ui->tableWidget->setItem(i,col++,new QTableWidgetItem( QString::number(i+18)));
}
//点击按钮 添加赵云
connect(ui->addBtn,&QPushButton::clicked,[=](){
//先判断有没有赵云,有不添加,没有才添加
bool isEmpty = ui->tableWidget->findItems("赵云",Qt::MatchExactly).empty();
if(isEmpty)
{
ui->tableWidget->insertRow(0);
ui->tableWidget->setItem(0,0,new QTableWidgetItem("赵云"));
ui->tableWidget->setItem(0,1,new QTableWidgetItem("男"));
ui->tableWidget->setItem(0,2,new QTableWidgetItem( QString::number(20)));
}
else
{
QMessageBox::warning(this,"警告!","赵云有了!");
}
});
//点击按钮 删除赵云
connect(ui->delBtn,&QPushButton::clicked,[=](){
bool isEmpty = ui->tableWidget->findItems("赵云",Qt::MatchExactly).empty();
if(isEmpty)
{
QMessageBox::warning(this,"警告!","赵云没有了!");
}
else
{
//先找到赵云所在的行
int row = ui->tableWidget->findItems("赵云",Qt::MatchExactly).first()->row();
//找到行数 删除掉
ui->tableWidget->removeRow(row);
}
});
}
Widget::~Widget()
{
delete ui;
}
main.cpp
#include "widget.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
return a.exec();
}
08_Control_Other
08_Control_Other.pro
#-------------------------------------------------
#
# Project created by QtCreator 2017-05-06T16:59:36
#
#-------------------------------------------------
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = 08_Control_Other
TEMPLATE = app
SOURCES += main.cpp\
widget.cpp
HEADERS += widget.h
FORMS += widget.ui
RESOURCES += \
res.qrc
widget.ui
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Widget</class>
<widget class="QWidget" name="Widget">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>705</width>
<height>634</height>
</rect>
</property>
<property name="windowTitle">
<string>Widget</string>
</property>
<widget class="QStackedWidget" name="stackedWidget">
<property name="geometry">
<rect>
<x>10</x>
<y>40</y>
<width>168</width>
<height>306</height>
</rect>
</property>
<property name="currentIndex">
<number>2</number>
</property>
<widget class="QWidget" name="page_4">
<layout class="QVBoxLayout" name="verticalLayout_2">
<item>
<widget class="QScrollArea" name="scrollArea">
<property name="widgetResizable">
<bool>true</bool>
</property>
<widget class="QWidget" name="scrollAreaWidgetContents">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>174</width>
<height>360</height>
</rect>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QPushButton" name="pushButton_12">
<property name="text">
<string>PushButton</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="pushButton">
<property name="text">
<string>PushButton</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="pushButton_3">
<property name="text">
<string>PushButton</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="pushButton_10">
<property name="text">
<string>PushButton</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="pushButton_9">
<property name="text">
<string>PushButton</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="pushButton_8">
<property name="text">
<string>PushButton</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="pushButton_7">
<property name="text">
<string>PushButton</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="pushButton_6">
<property name="text">
<string>PushButton</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="pushButton_5">
<property name="text">
<string>PushButton</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="pushButton_4">
<property name="text">
<string>PushButton</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="pushButton_11">
<property name="text">
<string>PushButton</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="pushButton_2">
<property name="text">
<string>PushButton</string>
</property>
</widget>
</item>
</layout>
</widget>
</widget>
</item>
</layout>
</widget>
<widget class="QWidget" name="page_6">
<layout class="QVBoxLayout" name="verticalLayout_3">
<item>
<widget class="QTabWidget" name="tabWidget">
<property name="currentIndex">
<number>2</number>
</property>
<widget class="QWidget" name="tab">
<attribute name="title">
<string>百度</string>
</attribute>
</widget>
<widget class="QWidget" name="tab_2">
<attribute name="title">
<string>谷歌</string>
</attribute>
</widget>
<widget class="QWidget" name="tab_3">
<attribute name="title">
<string>传智</string>
</attribute>
</widget>
</widget>
</item>
</layout>
</widget>
<widget class="QWidget" name="page_5">
<layout class="QVBoxLayout" name="verticalLayout_4">
<item>
<widget class="QToolBox" name="toolBox">
<property name="currentIndex">
<number>2</number>
</property>
<widget class="QWidget" name="page">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>150</width>
<height>210</height>
</rect>
</property>
<attribute name="label">
<string>家人</string>
</attribute>
</widget>
<widget class="QWidget" name="page_2">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>150</width>
<height>210</height>
</rect>
</property>
<attribute name="label">
<string>朋友</string>
</attribute>
</widget>
<widget class="QWidget" name="page_3">
<attribute name="label">
<string>黑名单</string>
</attribute>
</widget>
</widget>
</item>
</layout>
</widget>
</widget>
<widget class="QPushButton" name="btnScroll">
<property name="geometry">
<rect>
<x>200</x>
<y>50</y>
<width>75</width>
<height>23</height>
</rect>
</property>
<property name="text">
<string>ScrollArea</string>
</property>
</widget>
<widget class="QPushButton" name="btnTab">
<property name="geometry">
<rect>
<x>200</x>
<y>110</y>
<width>75</width>
<height>23</height>
</rect>
</property>
<property name="text">
<string>TabWidget</string>
</property>
</widget>
<widget class="QPushButton" name="btnToolBox">
<property name="geometry">
<rect>
<x>200</x>
<y>170</y>
<width>75</width>
<height>23</height>
</rect>
</property>
<property name="text">
<string>ToolBox</string>
</property>
</widget>
<widget class="QComboBox" name="comboBox">
<property name="geometry">
<rect>
<x>30</x>
<y>370</y>
<width>69</width>
<height>22</height>
</rect>
</property>
</widget>
<widget class="QPushButton" name="btnChoose">
<property name="geometry">
<rect>
<x>20</x>
<y>430</y>
<width>75</width>
<height>23</height>
</rect>
</property>
<property name="text">
<string>选择拖拉机</string>
</property>
</widget>
<widget class="QFontComboBox" name="fontComboBox">
<property name="geometry">
<rect>
<x>120</x>
<y>380</y>
<width>213</width>
<height>22</height>
</rect>
</property>
</widget>
<widget class="QLineEdit" name="lineEdit">
<property name="geometry">
<rect>
<x>120</x>
<y>440</y>
<width>113</width>
<height>20</height>
</rect>
</property>
<property name="echoMode">
<enum>QLineEdit::PasswordEchoOnEdit</enum>
</property>
</widget>
<widget class="QTextEdit" name="textEdit">
<property name="geometry">
<rect>
<x>110</x>
<y>480</y>
<width>104</width>
<height>71</height>
</rect>
</property>
</widget>
<widget class="QPlainTextEdit" name="plainTextEdit">
<property name="geometry">
<rect>
<x>240</x>
<y>480</y>
<width>104</width>
<height>71</height>
</rect>
</property>
</widget>
<widget class="QLabel" name="img">
<property name="geometry">
<rect>
<x>320</x>
<y>10</y>
<width>341</width>
<height>271</height>
</rect>
</property>
<property name="text">
<string>TextLabel</string>
</property>
</widget>
<widget class="QLabel" name="movie">
<property name="geometry">
<rect>
<x>380</x>
<y>310</y>
<width>261</width>
<height>181</height>
</rect>
</property>
<property name="text">
<string/>
</property>
</widget>
</widget>
<layoutdefault spacing="6" margin="11"/>
<resources/>
<connections/>
</ui>
widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
namespace Ui {
class Widget;
}
class Widget : public QWidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = 0);
~Widget();
private:
Ui::Widget *ui;
};
#endif // WIDGET_H
widget.cpp
#include "widget.h"
#include "ui_widget.h"
#include <QMovie>
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
//设置默认选中第0项
ui->stackedWidget->setCurrentIndex(0);
//stackWidget
connect(ui->btnScroll,&QPushButton::clicked,[=](){
ui->stackedWidget->setCurrentIndex(0);
});
connect(ui->btnTab,&QPushButton::clicked,[=](){
ui->stackedWidget->setCurrentIndex(1);
});
connect(ui->btnToolBox,&QPushButton::clicked,[=](){
ui->stackedWidget->setCurrentIndex(2);
});
//下拉框使用
ui->comboBox->addItem("奔驰");
ui->comboBox->addItem("宝马");
ui->comboBox->addItem("拖拉机");
//点击拖拉机按钮
connect(ui->btnChoose,&QPushButton::clicked,[=](){
ui->comboBox->setCurrentText("拖拉机");
});
//利用QLabel显示图片
ui->img->setPixmap(QPixmap(":/Image/Luffy.png"));
//利用QLabel显示gif图片
QMovie * movie = new QMovie(":/Image/mario.gif");
ui->movie->setMovie(movie);
//播放gif
movie->start();
}
Widget::~Widget()
{
delete ui;
}
main.cpp
#include "widget.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
return a.exec();
}
res.qrc
<RCC>
<qresource prefix="/">
<file>Image/butterfly.png</file>
<file>Image/butterfly1.png</file>
<file>Image/down.png</file>
<file>Image/Frame.jpg</file>
<file>Image/Luffy.png</file>
<file>Image/LuffyQ.png</file>
<file>Image/mario.gif</file>
<file>Image/OnePiece.png</file>
<file>Image/Sunny.jpg</file>
<file>Image/sunny.png</file>
<file>Image/up.png</file>
</qresource>
</RCC>