Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Strings to QTreeWidget
Qt 6.11 is out! See what's new in the release blog

Strings to QTreeWidget

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 3 Posters 842 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • jondoeJ Offline
    jondoeJ Offline
    jondoe
    wrote on last edited by jondoe
    #1

    I have 2 strings 1 button and 1 QTreeWidget.

    QString errors = "Error1,Error2,Error3,Error4"
    QString errorslinks = "www.link1.com,www.link2.com,www.link3.com,www.link4.com"

    And I have QTreeWidget.
    First I need split " , " QString errors after that I must add to QTreeWidget all errors like toplevel.
    And for example I selected Error1 and clicked button will open www.link1.com. Selected error2 clicked button will open www.link2.com.

    How can I do this can you help me please ?

    1 Reply Last reply
    0
    • T Offline
      T Offline
      TobbY
      wrote on last edited by
      #2

      Hi,
      Take a look at using QTreeWidget.
      https://blog.manash.me/quick-qt-6-how-to-add-qpushbutton-or-widgets-to-a-qtreewidget-as-qtreewidgetitem-2ae9f54c0e5f

      1 Reply Last reply
      1
      • Cobra91151C Offline
        Cobra91151C Offline
        Cobra91151
        wrote on last edited by Cobra91151
        #3

        Hi! Try my solution:

        Test.h:

        #ifndef TEST_H
        #define TEST_H
        
        #include <QWidget>
        #include <QHBoxLayout>
        #include <QVBoxLayout>
        #include <QTreeWidgetItem>
        #include <QDesktopServices>
        #include <QUrl>
        #include <QMessageBox>
        #include <QDebug>
        
        // The structure to maintain order: error => errorLink
        struct MyStruct {
            QString error;
            QString errorLink;
        };
        
        namespace Ui {
        class Test;
        }
        
        class Test : public QWidget
        {
            Q_OBJECT
        
        public:
            explicit Test(QWidget *parent = nullptr);
            QLayout *setMyLayout();
            QVector<MyStruct> addErrorData(MyStruct errorDataStruct, QVector<MyStruct> errorDataVector);
            void displayErrorData(QVector<MyStruct> errorDataVector);
            ~Test();
        
        private:
            Ui::Test *ui;
        };
        
        #endif // TEST_H
        

        Test.cpp:

        #include "test.h"
        #include "ui_test.h"
        
        Test::Test(QWidget *parent) :
            QWidget(parent),
            ui(new Ui::Test)
        {
            ui->setupUi(this);
            ui->treeWidget->setHeaderHidden(true);
            ui->treeWidget->setIndentation(0);
            ui->pushButton->setText("Load error link");
            // Lambda code executes the click signal of the button
            connect(ui->pushButton, &QPushButton::clicked, [this]() -> void {
                // Checks if the items is selected, otherwise displays the error
                if (ui->treeWidget->selectedItems().count() != NULL) {
                    // Displays the current selected item data and opens it with the default browser
                    QDesktopServices::openUrl(QUrl(ui->treeWidget->currentItem()->data(0, Qt::UserRole).toString()));
                } else {
                    QMessageBox::critical(this, "Error", "Please select the item from the list and try again!", QMessageBox::Ok);
                }
            });
        
            // Sets the main layout
            setLayout(setMyLayout());
        
            // Initialize the structure
            MyStruct errorDataStruct = {};
            // Initialize the vector with a structure
            QVector<MyStruct> errorDataVector;
            // Adds the error data to a vector structure and displays it in the QTreeWidget
            displayErrorData(addErrorData(errorDataStruct, errorDataVector));
        }
        
        QLayout *Test::setMyLayout()
        {
            QHBoxLayout *errorLayout = new QHBoxLayout();
            errorLayout->addWidget(ui->treeWidget);
        
            QHBoxLayout *buttonLayout = new QHBoxLayout();
            buttonLayout->addWidget(ui->pushButton, 1, Qt::AlignRight | Qt::AlignBottom);
        
            QVBoxLayout *testMainLayout = new QVBoxLayout();
            testMainLayout->addLayout(errorLayout);
            testMainLayout->addLayout(buttonLayout);
            return testMainLayout;
        }
        
        QVector<MyStruct> Test::addErrorData(MyStruct errorDataStruct, QVector<MyStruct> errorDataVector)
        {
            QString errors = "Error1,Error2,Error3,Error4";
            QStringList errorList = errors.split(",");
            QString errorsLinks = "www.cobratek.net,www.google.com,www.microsoft.com,www.qt.io";
            QStringList errorLinkList = errorsLinks.split(",");
        
            for (int i = 0; i < errorList.count(); i++) {
                 errorDataStruct.error = errorList[i];
        
                 for (int j = 0; j < errorLinkList.count(); j++) {
                      errorDataStruct.errorLink = errorLinkList[i];
                 }
        
                 errorDataVector.push_back(errorDataStruct);
            }
        
            return errorDataVector;
        }
        
        void Test::displayErrorData(QVector<MyStruct> errorDataVector)
        {
            for (MyStruct errorData : errorDataVector) {
                 QTreeWidgetItem *myTestItem = new QTreeWidgetItem(ui->treeWidget);
                 myTestItem->setText(0, errorData.error);
                 // adds error links to setData of the QTreeWidgetItem
                 myTestItem->setData(0, Qt::UserRole, errorData.errorLink);
            }
        
            errorDataVector.clear();
        }
        
        Test::~Test()
        {
            delete ui;
        }
        

        Test.ui:

        <?xml version="1.0" encoding="UTF-8"?>
        <ui version="4.0">
         <class>Test</class>
         <widget class="QWidget" name="Test">
          <property name="geometry">
           <rect>
            <x>0</x>
            <y>0</y>
            <width>400</width>
            <height>300</height>
           </rect>
          </property>
          <property name="windowTitle">
           <string>Test</string>
          </property>
          <widget class="QTreeWidget" name="treeWidget">
           <property name="geometry">
            <rect>
             <x>30</x>
             <y>20</y>
             <width>256</width>
             <height>192</height>
            </rect>
           </property>
           <column>
            <property name="text">
             <string notr="true">1</string>
            </property>
           </column>
          </widget>
          <widget class="QPushButton" name="pushButton">
           <property name="geometry">
            <rect>
             <x>320</x>
             <y>270</y>
             <width>75</width>
             <height>23</height>
            </rect>
           </property>
           <property name="text">
            <string>PushButton</string>
           </property>
          </widget>
         </widget>
         <layoutdefault spacing="6" margin="11"/>
         <resources/>
         <connections/>
        </ui>
        

        It will add all errors to the QTreeWidget as the toplevel QTreeWidgetItems's and open the appropriate error -> link by clicking the button. Happy coding!

        1 Reply Last reply
        2

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved