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. Need help on QListView or QListWidget
Forum Updated to NodeBB v4.3 + New Features

Need help on QListView or QListWidget

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 2 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.
  • D Offline
    D Offline
    deleted28
    wrote on last edited by deleted28
    #1

    Hello,

    I want to populate a QListView or(?) QListWidget with listitems containing a human readable QSring and a struct pointer.
    After selecting the QString I want to get back thecorresponding struct pointer. (similar to a combobox)

    Here is what I did to populate a QList:

    QList<QPair<QString, struct device_info*> > *ql = new QList<QPair<QString, struct device_info*> >;
    
    ql->append(qMakePair(string, current_device));
    
    

    Little code or link highly welcome, thx.

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @wally123 said:
      Hi if you use a QListWidget,
      you could use the items data property to store a pointer to the device_info with the Text
      Then when u select one, u have instance access to the structs.
      please see here
      http://stackoverflow.com/questions/7136818/can-i-store-some-user-data-in-every-item-of-a-qlistwidget

      1 Reply Last reply
      0
      • D Offline
        D Offline
        deleted28
        wrote on last edited by
        #3

        Exactly what I ask for, thank you very much !

        1 Reply Last reply
        0
        • D Offline
          D Offline
          deleted28
          wrote on last edited by
          #4

          Anyway, I can not build it.

                  QListWidgetItem *newItem = new QListWidgetItem;
                  QVariant qv1(current_device);
                  newItem->setData(Qt::UserRole, qv1);
                  newItem->setText(string);
                  ui->lw_device_list->insertItem(row, newItem);
          

          error message

          /opt/Qt/5.5/gcc_64/include/QtCore/qvariant.h:465: error: 'QVariant::QVariant(void*)' is private
               inline QVariant(void *) Q_DECL_EQ_DELETE;
                      ^
          
          struct device_info* current_device
          
          1 Reply Last reply
          0
          • mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by mrjj
            #5

            @wally123 said:
            is current_device a void * ?
            device_info is just normal struct yes?

            You will need to typecast to get QVariant to eat it.
            Maybe you can have Qlist with structs and just store the index to qlist.
            That may be more safe than say cast to void * and back to pointer to device_info
            else see here
            http://blog.bigpixel.ro/2010/04/storing-pointer-in-qvariant/

            QVariant qv1= qVariantFromValue((void *) current_device);

            U can get friends with QVariant here
            http://doc.qt.io/qt-5/qvariant.html#QVariant-1

            1 Reply Last reply
            0
            • D Offline
              D Offline
              deleted28
              wrote on last edited by deleted28
              #6

              **Current_device is a pointer to a struct device_info **

              maybe something like :

                      QVariant qv1;
                      qv1.setValue(current_device);
              

              no, does not work

              mrjjM 1 Reply Last reply
              0
              • D deleted28

                **Current_device is a pointer to a struct device_info **

                maybe something like :

                        QVariant qv1;
                        qv1.setValue(current_device);
                

                no, does not work

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

                @mrjj said:
                sorry I updated post.
                QVariant qv1= qVariantFromValue((void *) current_device);

                update:
                oh qv1.setValue seems fine too.

                D 1 Reply Last reply
                0
                • mrjjM mrjj

                  @mrjj said:
                  sorry I updated post.
                  QVariant qv1= qVariantFromValue((void *) current_device);

                  update:
                  oh qv1.setValue seems fine too.

                  D Offline
                  D Offline
                  deleted28
                  wrote on last edited by
                  #8

                  @mrjj

                  Yup !
                  thx

                  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 Extensions
                  • Unsolved