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

Get inherited QTreeWidgetItem

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 4 Posters 1.3k 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.
  • M Offline
    M Offline
    Mihaill
    wrote on last edited by Mihaill
    #1

    Hi!
    I inherited in QTreeWidgetItem and set this item to QTreeWidget. How I can get MyTreeWidgetItem from QTreeWidget in signal currentItemChanged and function currentItem()?

    class MyTreeWidgetItem : public QObject, public QTreeWidgetItem
    {
        Q_OBJECT
    public:
        MyTreeWidgetItem(){};
        int id = -1;
        int type = TypeItemProtocol::protocol;
        QVector <int> vector;
        void setId(int value){id = value;}
        int  getId(){return id;}
        void setType(int typeValue) {type = typeValue;}
        int  getType() {return type;}
        void addInVector(int value) {vector.append(value);}
    };
    
    jsulmJ 1 Reply Last reply
    0
    • M Mihaill

      Hi!
      I inherited in QTreeWidgetItem and set this item to QTreeWidget. How I can get MyTreeWidgetItem from QTreeWidget in signal currentItemChanged and function currentItem()?

      class MyTreeWidgetItem : public QObject, public QTreeWidgetItem
      {
          Q_OBJECT
      public:
          MyTreeWidgetItem(){};
          int id = -1;
          int type = TypeItemProtocol::protocol;
          QVector <int> vector;
          void setId(int value){id = value;}
          int  getId(){return id;}
          void setType(int typeValue) {type = typeValue;}
          int  getType() {return type;}
          void addInVector(int value) {vector.append(value);}
      };
      
      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @Mihaill Simply cast QTreeWidgetItem* to MyTreeWidgetItem*

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1
      • M Offline
        M Offline
        Mihaill
        wrote on last edited by
        #3

        It is work

        void MainWindow::on_treeWidgetSession_currentItemChanged(QTreeWidgetItem *current, QTreeWidgetItem *previous)
        {
            MyTreeWidgetItem* item = static_cast<MyTreeWidgetItem*>(current);
        
        jsulmJ 1 Reply Last reply
        0
        • M Mihaill

          It is work

          void MainWindow::on_treeWidgetSession_currentItemChanged(QTreeWidgetItem *current, QTreeWidgetItem *previous)
          {
              MyTreeWidgetItem* item = static_cast<MyTreeWidgetItem*>(current);
          
          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @Mihaill You should always check pointers after casts! If cast fails because the object is not MyTreeWidgetItem you will get nullptr and your app will crash if you try to use it.

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          Christian EhrlicherC JonBJ 2 Replies Last reply
          1
          • jsulmJ jsulm

            @Mihaill You should always check pointers after casts! If cast fails because the object is not MyTreeWidgetItem you will get nullptr and your app will crash if you try to use it.

            Christian EhrlicherC Offline
            Christian EhrlicherC Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @jsulm said in Get inherited QTreeWidgetItem:

            You should always check pointers after casts!

            ... and use the correct cast - static_cast<> is very problematic here since you can't huarantee that every item is a MyTreeWidgetItem here. Use dynamic_cast<> or qobject_cast<>

            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
            Visit the Qt Academy at https://academy.qt.io/catalog

            JonBJ 1 Reply Last reply
            2
            • jsulmJ jsulm

              @Mihaill You should always check pointers after casts! If cast fails because the object is not MyTreeWidgetItem you will get nullptr and your app will crash if you try to use it.

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by JonB
              #6

              @jsulm said in Get inherited QTreeWidgetItem:

              If cast fails because the object is not MyTreeWidgetItem you will get nullptr

              True for dynamic_cast<>. The only time OP's static_cast<> would return nullptr is if QTreeWidgetItem *current == nullptr, not if it were say plain QTreeWidgetItem *. Just saying.....

              1 Reply Last reply
              0
              • Christian EhrlicherC Christian Ehrlicher

                @jsulm said in Get inherited QTreeWidgetItem:

                You should always check pointers after casts!

                ... and use the correct cast - static_cast<> is very problematic here since you can't huarantee that every item is a MyTreeWidgetItem here. Use dynamic_cast<> or qobject_cast<>

                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on last edited by JonB
                #7

                @Christian-Ehrlicher said in Get inherited QTreeWidgetItem:

                or qobject_cast<>

                => qobject_cast<MyTreeWidgetItem*>(current);

                Here MyTreeWidgetItem is QObject but current's formal type is QTreeWidgetItem, which is not QObject. In this situation is qobject_cast<> allowed?

                1 Reply Last reply
                0

                • Login

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