Qt教程 笔记2

  1. design form本来就是只会生成一个.ui文件。
    Qt设计师界面类才是生成 .h .cpp .ui 三个文件的。

  2. 字符串格式化

方法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());
    }
  1. 设置字体颜色

    第一种,使用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>");
  1. 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); //记得返回基类的事件处理函数

    }

猜你喜欢

转载自blog.51cto.com/4198767/2584423