-
design form本來就是隻會生成一個.ui文件。
Qt設計師界面類纔是生成 .h .cpp .ui 三個文件的。 - 字符串格式化
方法1:
QString result;
QTextStream(&result) << "pi = " << 3.14;
qDebug()<< result;
QString s1("1234567890");
int n = 123;
方法2:
QString strFormat1;
strFormat1.sprintf(("string1 %s %d"),s1.data(), n);
qDebug()<< strFormat1;
方法3:
QString strFormat2 = QString::asprintf ("格式化字符串2:%s 秒,%d 毫秒", s1.data(), n);
qDebug()<< strFormat2;
方法4:
QString strFormat3 = QString("字符串3 %1 %2").arg(s1).arg(n);
qDebug()<< strFormat3;
方法5:
QStringList strList;
for(int i=0; i < 3; i++)
{
strList.append(s1);
qDebug()<< strList.join(QString());
}
-
設置字體顏色
第一種,使用setPalette()
QLabel *label = new QLabel(tr("Hello Qt!")); QPalette palet; palet.setColor(QPalette::WindowText,Qt::white); label->setPalette(palet);
第二種,使用樣式表如下:
QLabel *labMsg = new QLabel;
// QFont *font = new QFont( "Timers" , 14 , QFont::Bold) ;
QFont font("Microsoft YaHei", 12, QFont::Normal); //設置爲微軟雅黑,字體12號,加粗
labMsg->setStyleSheet("color:blue;"); //設置爲藍色
labMsg->setFont(font);
或
lab->setStyleSheet("font-size:36px;color:red;font-weight:bold;fontstyle:italic");
第三種,使用一些簡單的HTML格式:
QLabel *label = new QLabel(tr("Hello Qt!"));
QLabel *label = new QLabel("<h2><i>Hello</i><font color=red>Qt!</font></h2>");
- QListWidget 中 CheckboBox 選中狀態 用 checkState() 判斷
設置反選if(aItem->checkState()) { aItem->setCheckState(Qt::Unchecked);//設置爲未選中 } else { aItem->setCheckState(Qt::Checked);//設置爲選中 }
5. 固定對話框大小
**方法1:**
QLayout::SetFixedSize
**方法2:** **maximumSize** 與** minimumSize **設置成相同的值
調整窗口大小
this->resize( QSize( 600, 400 ));
6. 控件隨窗口大小而改變
1) 在窗體中拖放一 QPlainTextEdit 控件,至於窗口中
2) 在窗口中,QPlainTextEdit 控件外右擊,選擇佈局/柵格佈局
這樣 QPlainTextEdit 控件可隨着窗口大小改變
改變窗口大小觸發事件 resizeEvent
1) 在頭文件中聲明
virtual void resizeEvent(QResizeEvent *event) override;
2) 源文件中 實現
void MainWindow::resizeEvent(QResizeEvent *event){}
7. 界面設置 Layout 比例,設置屬性 layoutStretch 值
代碼設置 **QGridLayout** 行列比例
QGridLayout::setRowStretch(行碼, 比例值);
QGridLayout::setColumnStretch(列碼, 比例值);
代碼設置 QVBoxLayout 行高比例
ui->verlLayout->setStretchFactor(ui->txtEdit1,1);
ui->verlLayout->setStretchFactor(ui->txtEdit2,2);
8. QMainWindow 中代碼添加控件不顯示
QVBoxLayout *Layout = new QVBoxLayout();
QPushButton* btn1 = new QPushButton("001");
btn1->setMinimumWidth(200);
btn1->setMinimumHeight(50);
QPushButton* btn2 = new QPushButton("002");
Layout->addWidget(btn1);
Layout->addWidget(btn2);
Layout->setStretchFactor(btn1, 2);
Layout->setStretchFactor(btn2, 2);
QWidget *center = new QWidget(this);
setCentralWidget(center);
center->setLayout(Layout); //設置佈局
說明:在mainwindow中需要添加中心部件,然後將我們添加的佈局用中心部件進行設置
也可以如下方式設置:
this->centralWidget()->setLayout(layout);
9. QtCreator 添加資源
1) 右擊項目名稱/Add New/
2) 選擇一個模板 頁面
(1) 左側選擇 Qt
(2) 右側選擇 Qt Resource File
(3) 點擊 Choose... 按鈕
3) Qt Resource File
(1) 輸入資源文件名稱(img)
(2) 路徑
(3) 下一步
4) 完成/
5) 點擊中間窗口下部的 "添加" 按鈕,選擇 "添加前綴"
(1) 前綴默認值爲 "/new/prefix1",改爲 "/"
(2) 點擊中間窗口下部的 "添加" 按鈕,選擇 "添加文件"
(3) 選擇當前工程目錄下 images 文件夾中 config.ico 圖片
6) 文件 / 保存所有文件
也可以在第4不完成後 右擊 img.qrc 選擇 "添加現有文件..."
在彈出對話框中選擇 ico 圖標 點擊"打開"
** 刪除 資源文件**
編輯/.pro 文件中 刪除下面內容
RESOURCES += img.qrc
** 刪除添加圖片文件**
右擊"/"文件圖標,選擇"Remove prefix"
10. 添加工具欄按鈕
1) ui 界面 選擇 "Action Editor"
2) 點擊文件圖標(提示爲"新建")
3) 彈出"新建動作"窗口
(1) 文本:輸入按鈕名稱
(2) 對象名稱:即控件名
(3) ToolTip: 按鈕獲取鼠標焦點時提示
(4) 圖標: 點擊"..."按鈕,選擇圖標
(5) 先點擊 "Shortcut" 編輯框,按快捷鍵"Ctr+C"
(6) 點擊 "OK"
在 Action Editor 窗口出現剛添加的action
4) 選擇 action 拖拽到工具欄即可(窗口標題欄下方)
5) 設置 toolButtonStyle 爲 ToolButtonTextBesideIcon
11. action 觸發槽函數 void QAction::triggered ( bool checked = false )
設置爲可選狀態,勾選上"Checkable(可選的)", 參數 checked 的值才能在true間false切換
12. Qt 獲取當前程序運行路徑
#include <QCoreApplication>
//獲取程序當前運行目錄
QString fileName = QCoreApplication::applicationDirPath();
13. Qt 5.9.8 QTcpServer 虛函數 **void incomingConnection(int socketDescriptor)** 未被調用
改爲
void incomingConnection(qintptr socketDescriptor);
即可
14. 命令行下編譯Qt程序
1) MyHello 文件加下添加 hello.cpp 內容如下:
#include <QApplication>
#include <QLabel>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QLabel *label = new QLabel("Hello Qt!");
label->show();
return app.exec();
}
2) 命令行切換至 MyHello 文件目錄下,輸入 **qmake -project **命令
MyHello 文件夾下**生成 MyHello.pro **
3) MyHello.pro 文件添加以下內容
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
4) 命令行執行 **qmake** 或 **qmake MyHello.pro**
**生成 Makefile 文件**
5) windows上,如果使用的是開源版的Qt,用**mingw32-make.exe**;如果是商業版的Qt,用**nmake**
6) 通過 MyHello.pro 文件創建一個 Visual Studio 的工程文件 MyHello.vcxproj,輸入以下命令
**qmake -tp vc MyHello.pro**
/************************************************************/
15. Qt5 運行 Qt4 程序
1) 工程文件添加
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
2) 將代碼
#include <QtGui>
改爲
#include <QtWidgets>
3) "fatal error LNK1112: 模塊計算機類型“x64”與目標計算機類型“X86”衝突"
64位(我的電腦/屬性/系統類型/64位操作系統) Qt Creator 中修改項目配置
項目(左側)/Build & Run /選擇 "Desktop Qt 5.9.8 MSVC2013 64bit"/
https://wiki.qt.io/Transition_from_Qt_4.x_to_Qt5
16. UI界面添加自定義控件
1) 新建工程 CustomListWidgetDemo
2) 添加 類 CustomListWidget 繼承自 QListWidget
3) CustomListWidget類中自定義一鼠標事件函數:mousePressEvent
4) ui 界面拖拽放入一 QListWidget
5) 右擊 QListWidget 控件選擇 "提升爲(Promote to...)"
Base class name: 選擇 QListWidget
Promoted class name:CustomListWidget
Header file:CustomListWidget.h
點擊 添加(Add)
** 刪除 Debug 文件夾 重新生成**
CustomListWidget.h 文件內容如下:
#ifndef CUSTOMLISTWIDGET_H
#define CUSTOMLISTWIDGET_H
#include <QWidget>
#include <QListWidget>
class CustomListWidget : public QListWidget
{
Q_OBJECT
public:
CustomListWidget(QWidget *parent = nullptr);
protected:
void mousePressEvent(QMouseEvent *event);
};
#endif // CUSTOMLISTWIDGET_H
CustomListWidget.cpp 文件內容如下:
#include "CustomListWidget.h"
#include <QMouseEvent>
#include <QDebug>
CustomListWidget::CustomListWidget(QWidget *parent)
:QListWidget(parent)
{
}
void CustomListWidget::mousePressEvent(QMouseEvent *event)
{
if(event->button()==Qt::LeftButton)
{
qDebug()<<QString("table mouse press event %1,%2").arg(event->pos().rx()).arg(event->pos().ry());
}
return QListWidget::mousePressEvent(event); //記得返回基類的事件處理函數
}