日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Qt(二)

發布時間:2023/12/9 编程问答 18 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Qt(二) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

文章目錄

    • 1. QMainWindow
    • 2. 添加資源文件
    • 3. 對話框
    • 4. QMessageBox
    • 5. QFileDialog
    • 6. QColorDialog
    • 7. 界面布局
    • 8. 控件

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設置 ??俊⒏印⒁苿?br /> 1.2.4添加菜單項
1.3狀態欄 statusBar 只能一個
1.3.1左側添加
1.3.2右側添加
1.4鉚接部件 可以多個
1.4.1QDockWidget
1.4.2addDockWidget(默認位置,。。)
1.4.3設置后期的??课恢?br /> 1.5核心部件 只能一個
1.5.1setCentralWidget

01_QMainWindow.pro

#------------------------------------------------- # # Project created by QtCreator 2017-05-06T09:20:20 # #-------------------------------------------------QT += core guigreaterThan(QT_MAJOR_VERSION, 4): QT += widgetsTARGET = 01_QMainWindow TEMPLATE = appSOURCES += main.cpp\mainwindow.cppHEADERS += mainwindow.h

mainwindow.h

#ifndef MAINWINDOW_H #define MAINWINDOW_H#include <QMainWindow>class MainWindow : public QMainWindow {Q_OBJECTpublic: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); //默認停靠范圍//只允許左右側???/span>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);//設置??糠秶?/span>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 guigreaterThan(QT_MAJOR_VERSION, 4): QT += widgetsTARGET = 02_Source TEMPLATE = appSOURCES += main.cpp\mainwindow.cppHEADERS += mainwindow.hFORMS += mainwindow.uiRESOURCES += \res.qrc

mainwindow.h

#ifndef MAINWINDOW_H #define MAINWINDOW_H#include <QMainWindow>namespace Ui { class MainWindow; }class MainWindow : public QMainWindow {Q_OBJECTpublic: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 guigreaterThan(QT_MAJOR_VERSION, 4): QT += widgetsTARGET = 03_QDialog TEMPLATE = appSOURCES += main.cpp\mainwindow.cppHEADERS += mainwindow.hFORMS += mainwindow.uiCONFIG += 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_OBJECTpublic: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 guigreaterThan(QT_MAJOR_VERSION, 4): QT += widgetsTARGET = 04_Layout TEMPLATE = appSOURCES += main.cpp\widget.cppHEADERS += widget.hFORMS += widget.ui

widget.h

#ifndef WIDGET_H #define WIDGET_H#include <QWidget>namespace Ui { class Widget; }class Widget : public QWidget {Q_OBJECTpublic: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 guigreaterThan(QT_MAJOR_VERSION, 4): QT += widgetsTARGET = 05_Control TEMPLATE = appSOURCES += main.cpp\widget.cppHEADERS += widget.hFORMS += widget.uiRESOURCES += \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_OBJECTpublic: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 guigreaterThan(QT_MAJOR_VERSION, 4): QT += widgetsTARGET = 06_Control_TreeWidget TEMPLATE = appSOURCES += main.cpp\widget.cppHEADERS += widget.hFORMS += 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_OBJECTpublic: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 << "巫醫" << "后排輔助法師,可以使用奇特的巫術詛咒敵人與治療隊友";//追加子項目 子項也是QTreeWidgetItemQTreeWidgetItem * 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 guigreaterThan(QT_MAJOR_VERSION, 4): QT += widgetsTARGET = 07_Control_TableWidget TEMPLATE = appSOURCES += main.cpp\widget.cppHEADERS += widget.hFORMS += 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_OBJECTpublic: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 numberui->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 guigreaterThan(QT_MAJOR_VERSION, 4): QT += widgetsTARGET = 08_Control_Other TEMPLATE = appSOURCES += main.cpp\widget.cppHEADERS += widget.hFORMS += widget.uiRESOURCES += \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_OBJECTpublic: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);//stackWidgetconnect(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);//播放gifmovie->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>

總結

以上是生活随笔為你收集整理的Qt(二)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。