• qt-C++笔记之treeWidget初次使用


    qt-C++笔记之treeWidget初次使用

    code review!

    1.运行

    https://note.youdao.com/s/RWcKoOO1

    2.文件结构

    在这里插入图片描述

    3.main.cpp

    在这里插入图片描述

    代码

    #include "widget.h"
    
    #include 
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        Widget w;
        w.show();
        return a.exec();
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    4.widget.h

    在这里插入图片描述

    代码

    #ifndef WIDGET_H
    #define WIDGET_H
    
    #include 
    #include 
    
    QT_BEGIN_NAMESPACE
    namespace Ui { class Widget; }
    QT_END_NAMESPACE
    
    class Widget : public QWidget
    {
        Q_OBJECT
    
    public:
        Widget(QWidget *parent = nullptr);
        ~Widget();
    
    private:
        Ui::Widget *ui;
    };
    #endif // WIDGET_H
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    5.widget.cpp

    在这里插入图片描述

    代码

    #include "widget.h"
    #include "ui_widget.h"
    
    Widget::Widget(QWidget *parent)
        : QWidget(parent)
        , ui(new Ui::Widget)
    {
        ui->setupUi(this);
    
        ui->treeWidget->setHeaderLabels(QStringList()<<"key"<<"value");
        QTreeWidgetItem* item1=new QTreeWidgetItem(QStringList()<<"Global Options");
        ui->treeWidget->addTopLevelItem(item1);
        item1->setIcon(0,QIcon(":/image/options.png"));
    
        ui->treeWidget->setColumnWidth(0, 300);
        ui->treeWidget->setColumnWidth(1, 300);
    
        QTreeWidgetItem* item1_child1=new QTreeWidgetItem(QStringList()<<"Fixed Frame1");
        item1->addChild(item1_child1);
    
        QComboBox* box1_1=new QComboBox;
        box1_1->setMaximumWidth(200);
        box1_1->addItem("base_link");
        box1_1->addItem("odom");
        box1_1->addItem("map");
        box1_1->setEditable(true);
        ui->treeWidget->setItemWidget(item1_child1,1,box1_1);
    
        QTreeWidgetItem* item1_child2=new QTreeWidgetItem(QStringList()<<"Fixed Frame2");
        item1->addChild(item1_child2);
    
        QComboBox* box2_1=new QComboBox;
        box2_1->setMaximumWidth(200);
        box2_1->addItem("map");
        box2_1->addItem("odom");
        box2_1->addItem("base_link");
        box2_1->setEditable(true);
        ui->treeWidget->setItemWidget(item1_child2,1,box2_1);
    }
    
    Widget::~Widget()
    {
        delete ui;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44

    6.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>672</width>
        <height>541</height>
       </rect>
      </property>
      <property name="windowTitle">
       <string>Widget</string>
      </property>
      <widget class="QTreeWidget" name="treeWidget">
       <property name="geometry">
        <rect>
         <x>40</x>
         <y>30</y>
         <width>601</width>
         <height>481</height>
        </rect>
       </property>
       <column>
        <property name="text">
         <string notr="true">1</string>
        </property>
       </column>
      </widget>
     </widget>
     <resources/>
     <connections/>
    </ui>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34

    7.main.qrc

    <RCC>
        <qresource prefix="/">
            <file>image/options.png</file>
        </qresource>
    </RCC>
    
    • 1
    • 2
    • 3
    • 4
    • 5

    8.qt_widget_test.pro

    QT       += core gui
    
    greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
    
    CONFIG += c++11
    
    # The following define makes your compiler emit warnings if you use
    # any Qt feature that has been marked deprecated (the exact warnings
    # depend on your compiler). Please consult the documentation of the
    # deprecated API in order to know how to port your code away from it.
    DEFINES += QT_DEPRECATED_WARNINGS
    
    # You can also make your code fail to compile if it uses deprecated APIs.
    # In order to do so, uncomment the following line.
    # You can also select to disable deprecated APIs only up to a certain version of Qt.
    #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0
    
    SOURCES += \
        main.cpp \
        widget.cpp
    
    HEADERS += \
        widget.h
    
    FORMS += \
        widget.ui
    
    # Default rules for deployment.
    qnx: target.path = /tmp/$${TARGET}/bin
    else: unix:!android: target.path = /opt/$${TARGET}/bin
    !isEmpty(target.path): INSTALLS += target
    
    RESOURCES += \
        main.qrc
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34

    9.options.png

    在这里插入图片描述

  • 相关阅读:
    记录一个HttpClient超时连接配置不生效的问题排查过程
    内存空间的分配与回收之连续分配管理方式
    Qt扫盲-QDataStream 序列化和反序列化理论
    C语言|递归|青蛙跳台阶和汉诺塔问题
    React源码分析1-jsx转换及React.createElement
    孩子用台灯哪种好用?热门好用的全新护眼台灯推荐
    rk版本编译系统
    【计算机毕业设计】094图书馆自习室座位预约管理微信小程序
    09.06app端自动化
    Spire.Office for Java 7.9.9 ---2022-09-30
  • 原文地址:https://blog.csdn.net/weixin_43297891/article/details/134486271