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. How to make a QVector of a class usable for ui?
Qt 6.11 is out! See what's new in the release blog

How to make a QVector of a class usable for ui?

Scheduled Pinned Locked Moved Solved General and Desktop
10 Posts 5 Posters 2.7k Views 2 Watching
  • 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.
  • NiagarerN Offline
    NiagarerN Offline
    Niagarer
    wrote on last edited by
    #1

    First things first: I'm very new in Qt, so it definately can be a stupid question.
    I tried to make a QVector that contains Objects of a specific class. Problem here:

    1. I cant append dinamically new items (or I am just too stupid) like
    myVector.append( new myClass(this) );
    
    1. I cant use the items from the QVector to setup ui like this:
    ui->myGraph->addTab( myVector.last(), "xyz" );
    

    (I also tried to do this stuff with a QVector of pointers etc. but I just got more problems)

    Any Ideas?
    Thanks for answers!

    jsulmJ 1 Reply Last reply
    0
    • artwawA Offline
      artwawA Offline
      artwaw
      wrote on last edited by
      #2

      Hi.
      You can store pointers to objects this way. In example:

      MyClass *myObj = new MyClass(this);
      myVector.append(myObj);
      

      However the question is: what exactly would you like to get from 2)? I mean - what's your use case?

      For more information please re-read.

      Kind Regards,
      Artur

      NiagarerN 1 Reply Last reply
      0
      • NiagarerN Niagarer

        First things first: I'm very new in Qt, so it definately can be a stupid question.
        I tried to make a QVector that contains Objects of a specific class. Problem here:

        1. I cant append dinamically new items (or I am just too stupid) like
        myVector.append( new myClass(this) );
        
        1. I cant use the items from the QVector to setup ui like this:
        ui->myGraph->addTab( myVector.last(), "xyz" );
        

        (I also tried to do this stuff with a QVector of pointers etc. but I just got more problems)

        Any Ideas?
        Thanks for answers!

        jsulmJ Offline
        jsulmJ Offline
        jsulm
        Lifetime Qt Champion
        wrote on last edited by
        #3

        @Niagarer You should show more code: what is myVector exactly?
        "I cant append dinamically new items (or I am just too stupid) like" - what exactly is the problem/error?

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

        NiagarerN 1 Reply Last reply
        0
        • NiagarerN Offline
          NiagarerN Offline
          Niagarer
          wrote on last edited by
          #4
          This post is deleted!
          1 Reply Last reply
          0
          • artwawA artwaw

            Hi.
            You can store pointers to objects this way. In example:

            MyClass *myObj = new MyClass(this);
            myVector.append(myObj);
            

            However the question is: what exactly would you like to get from 2)? I mean - what's your use case?

            NiagarerN Offline
            NiagarerN Offline
            Niagarer
            wrote on last edited by
            #5

            @artwaw
            Thanks for the answer!
            The problem here is, that I want to add new items dynamically. When I click a button, it should add a new item.
            because of that, I tried to do it the way I showed above (with no specific name, because I dont want to change an existing item, I want to create a new one).

            But I still can not append it to my vector. it says:

            error: no matching function for call to 'QVector<myClass>::append(myClass*&)'
            myClasses.append(testMyClass);
            

            The second problem is, that it says

            error: no matching function for call to 'QTabWidget::addTab(myClass&, QString&)'
            ui->Graph->addTab(myClasses.last(), "xyz");
            

            when I try to use the last item of myClasses to set the widget (myClass is a widgetClass) of a new tab.

            Kind Regards!

            J.HilkJ 1 Reply Last reply
            0
            • NiagarerN Niagarer

              @artwaw
              Thanks for the answer!
              The problem here is, that I want to add new items dynamically. When I click a button, it should add a new item.
              because of that, I tried to do it the way I showed above (with no specific name, because I dont want to change an existing item, I want to create a new one).

              But I still can not append it to my vector. it says:

              error: no matching function for call to 'QVector<myClass>::append(myClass*&)'
              myClasses.append(testMyClass);
              

              The second problem is, that it says

              error: no matching function for call to 'QTabWidget::addTab(myClass&, QString&)'
              ui->Graph->addTab(myClasses.last(), "xyz");
              

              when I try to use the last item of myClasses to set the widget (myClass is a widgetClass) of a new tab.

              Kind Regards!

              J.HilkJ Offline
              J.HilkJ Offline
              J.Hilk
              Moderators
              wrote on last edited by mrjj
              #6

              @Niagarer you haven't jet shown us, how you create/define your QVector.

              something like this:

              QVector<QWidget*> myVector;
              
              QWidget *w = new QWidget(..);
              myVector.append(w);
              

              defenitly work.


              Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


              Q: What's that?
              A: It's blue light.
              Q: What does it do?
              A: It turns blue.

              1 Reply Last reply
              0
              • jsulmJ jsulm

                @Niagarer You should show more code: what is myVector exactly?
                "I cant append dinamically new items (or I am just too stupid) like" - what exactly is the problem/error?

                NiagarerN Offline
                NiagarerN Offline
                Niagarer
                wrote on last edited by
                #7

                @jsulm
                Yes, I'm sorry.
                myVector is a vector of myClass objects.
                myClass is a custom QWidget Class.
                I want to create a new object of myClass when I add a new tab in the window and store it in the QVector.

                mrjjM jsulmJ 2 Replies Last reply
                0
                • NiagarerN Niagarer

                  @jsulm
                  Yes, I'm sorry.
                  myVector is a vector of myClass objects.
                  myClass is a custom QWidget Class.
                  I want to create a new object of myClass when I add a new tab in the window and store it in the QVector.

                  mrjjM Offline
                  mrjjM Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  @Niagarer
                  hi
                  With Widgets the vector must be pointers.
                  Just as both @artwaw and @J-Hilk shows

                  You cannot store the actual widget in the list. must be with new and pointer.

                  1 Reply Last reply
                  0
                  • NiagarerN Niagarer

                    @jsulm
                    Yes, I'm sorry.
                    myVector is a vector of myClass objects.
                    myClass is a custom QWidget Class.
                    I want to create a new object of myClass when I add a new tab in the window and store it in the QVector.

                    jsulmJ Offline
                    jsulmJ Offline
                    jsulm
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    @Niagarer said in How to make a QVector of a class usable for ui?:

                    myVector is a vector of myClass objects

                    then why are you trying to append pointers?
                    It should be:

                    QVector<QWidget*> myVector;
                    

                    as @J-Hilk said.
                    Be aware that you should really use pointers as QObjects are not copyable.

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

                    1 Reply Last reply
                    3
                    • NiagarerN Offline
                      NiagarerN Offline
                      Niagarer
                      wrote on last edited by
                      #10

                      Ok, thank you all!
                      So, as you told me, I just had to use pointers.
                      When I useed normal objects (no pointers), I was not able to add the new item to the vector and use it as widget for the new tab.
                      The tab takes pointers, I'm sorry, I didn't see that (but, yup it makes sense).
                      So now, it looks like:

                      QVector<myClass*> myClasses;
                      myClass *testMyClass = new myClass(this);
                      myClasses->append(testMyClass);
                      ui->Graph->addTab( myClasses.last(), "xyz");
                      

                      and it works yaay!
                      Thanks for all answers!

                      1 Reply Last reply
                      1

                      • Login

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