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

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

code review!

文章目录

  • qt-C++笔记之treeWidget初次使用
    • 1.运行
    • 2.文件结构
    • 3.main.cpp
    • 4.widget.h
    • 5.widget.cpp
    • 6.widget.ui
    • 7.main.qrc
    • 8.qt_widget_test.pro
    • 9.options.png

1.运行

qt-C++笔记之treeWidget初次使用_第1张图片

2.文件结构

qt-C++笔记之treeWidget初次使用_第2张图片

3.main.cpp

qt-C++笔记之treeWidget初次使用_第3张图片

代码

#include "widget.h"

#include 

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Widget w;
    w.show();
    return a.exec();
}

4.widget.h

qt-C++笔记之treeWidget初次使用_第4张图片

代码

#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

5.widget.cpp

qt-C++笔记之treeWidget初次使用_第5张图片

代码

#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;
}

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>

7.main.qrc

<RCC>
    <qresource prefix="/">
        <file>image/options.png</file>
    </qresource>
</RCC>

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

9.options.png

在这里插入图片描述

你可能感兴趣的:(qt-C++程序笔记,qt,c++,笔记)