15

zqt_helper 轻松开发Qt5 Widgets应用

 3 years ago
source link: http://www.cnblogs.com/bbqzsl/p/12943804.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

目标:

1. 代码更加紧凑,所写即所到。

2. 代码层次更直观,直接反映界面窗口层次关系。

3. 不继承类,不重写虚函数,slot接收QEvent。

4. 简单写布局,忘掉api函数。

5. 免去一大堆临时变量的变量名。

思路:

operator () (QLayout*) 开始一个布局

operator () (QWidget*) 开始一个窗口元素,并将元素加入到布局

operator [] <T> (const T&) 设置布局或窗口元素的属性

operator [] (lambda) 设置一个元素的事件回调

zqt_helper项目地址 https://github.com/bbqz007/zhelper-qt5Widgets , 适用于Qt5 Widgets。

zwx_helper项目地址 https://github.com/bbqz007/zhelper-wxWidgets , 适用于 wxWidgets。

使用zqt_helper如何写Qt Widgets 布局:

layout::begin                            // 最外层垂直布局
    (new QVBoxLayout)
        [ QMargins() ]
        (layout::begin                    // 第一个水平布局
              (new QHBoxLayout)
                    (new QLineEdit)
                        ["input txt 1"]    // id
                        [ QSize() ]    // 设置大小
                    (new QPushButton)
                        ["input btn 1"]    // id
                        [ QSize() ]
                        [ ONSIGNAL(    // 设置slot,同时定义slot
                             &QPushButton::clicked,
                               [=] (bool) {
                                    // handle slot
                                })]
                (layout::end))
    
          (layout::begin                   //  第二个水平布局 
                (new QHBoxLayout)
                (new QListWidget)
                    ["list"] // id
                    [ QSize() ]
                 (layout::end))
    (layout::end,
        [=](QLayout*){
            // 完成布局后回调
    })  ;                               

基本上可以不调用具体QLayout和QWidget的函数,也免去了记住这些函数。没有声明过一个变量名。

如果希望更加细致的设置可以在 operator[ ] 设置ONLOAD回调:

(new QTableWidget)
    ["table 1"]    // id
    [ ONLOAD [=](QTableWidget* table) {
           // 设置table
            
     }]

Qt虽然有signal-slot机制,支持动态回调绑定,但是对于一些事件(QEvent 如输入设备,窗口管理等事件),你必须继承QWidget,重写对应的事件虚函数。这里提供一个ZQ模板类,将QWidget所有事件虚函数由ZQEmitter代理signal发射,也就是你可以通过slot接收这些事件,而不必去多写一个继承类。尤其是为了弹出菜单就要多写一个继承类。

用slot接收QEvent:

(new ZQ<QFrame>)
    ["frame 1"]     // id
    [ ONSIGNAL(
            &QFrame::contextMenuEvent,
            [=] (QContextMenuEvent*) {
                // 弹出菜单
     })]            

zqt_helper 同样支持 menu布局,如果你没有搞明QMenu和QAction,没有关系,你可以忘记。

menu::begin
    (new QMenu)
         ("level 1 item 1")
                   [ TRIGGERED [=]() {
                        // do sth
                    } ]            
         ("level 1 item 2")
                   [ TRIGGERED [=]() {
                        // do sth
                    } ]
         (menu::begin
                ("level 1 item 3")
                    ("level 2 item 1")
                    ("level 2 item 2")
                       [ TRIGGERED [=]() {
                            // do sth
                        } ]    
                (menu::end))
    (menu::end);    

zqt_helper支持QTableWidget

QTableWidget* table = new QTableWidget;
// 设置水平header
column::begin(table)
    ("header col 1")
        [ QFont() ]
        [ QColor() ]
    ("header col 2")
    ("header col 3")
    ("header col 4")
    (column::end);
// 添加行内容
row::begin(table)
    ("row1 col1")
    ("row1 col2")
    ("row1 col3")
    ("row1 col4")
    (row::another)
    ("row2 col1")
    ("row2 col2")
    ("row2 col3")
    ("row2 col4")
    (row::end);
// 修改单元
cell::begin(table)
    (1, 2)    // 定位
        [ "change@ row2 col3" ]
    (0, 3)
        [ "change@ row1 col4" ]
    (cell::end);

zqt_helper项目是zwx_helper项目的qt5版本,支持同样的编程方法。

zqt_helper项目地址 https://github.com/bbqz007/zhelper-qt5Widgets , 适用于Qt5 Widgets。

zwx_helper项目地址 https://github.com/bbqz007/zhelper-wxWidgets , 适用于 wxWidgets。

两个项目同样提供一个gui4smali的demo。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK