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 append CustomQWidget from a QVBoxLayout to QVector
Forum Updated to NodeBB v4.3 + New Features

How to append CustomQWidget from a QVBoxLayout to QVector

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 292 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.
  • E Offline
    E Offline
    Emanuele Papa
    wrote on last edited by Emanuele Papa
    #1

    Hi!

    I'm trying to get every CustomQWidget i added to the layout and insert them into a QVector so i can sort them.

    void MainWindow::onFilenameClick()
    {
        QVector<CustomQWidget*> qv;
    
        for(int i = 0; i < ui->widgets_layout->count(); i++) {
            QWidget *widget = ui->widgets_layout->itemAt(i)->widget();
            if(widget != nullptr) {
                qv.append(widget);
            }
        }
        std::sort(qv.begin(), qv.end(), _functor());
        update_ui(qv);
    }
    

    The problem is that i keep getting the error message

    no matching member function for call to 'append'
    candidate function not viable: cannot convert from base class pointer 'QWidget *' to derived class pointer 'CustomQLabel *const' for 1st argument
    candidate function not viable: cannot convert from base class pointer 'QWidget *' to derived class pointer 'CustomQLabel *' for 1st argument
    candidate function not viable: cannot convert from base class pointer 'QWidget *' to 'const QVector<CustomQWidget *>' for 1st argument
    

    I've already tryed to static_cast the CustomQWidget to QWidget but nothing has changed.

    qv.append(static_cast<QWidget*>(widget));
    

    And I also tried with

    qv.push_back(widget);
    qv.insert(i, widget);
    

    Here's the CustomQWidget.h

    class CustomQWidget : public QWidget
    {
      Q_OBJECT
    public:
    ...
    private:
      Ui::CustomQWidget *ui;
    ...
    };
    

    I really don't know what to do, please help me

    JonBJ 1 Reply Last reply
    0
    • E Emanuele Papa

      Hi!

      I'm trying to get every CustomQWidget i added to the layout and insert them into a QVector so i can sort them.

      void MainWindow::onFilenameClick()
      {
          QVector<CustomQWidget*> qv;
      
          for(int i = 0; i < ui->widgets_layout->count(); i++) {
              QWidget *widget = ui->widgets_layout->itemAt(i)->widget();
              if(widget != nullptr) {
                  qv.append(widget);
              }
          }
          std::sort(qv.begin(), qv.end(), _functor());
          update_ui(qv);
      }
      

      The problem is that i keep getting the error message

      no matching member function for call to 'append'
      candidate function not viable: cannot convert from base class pointer 'QWidget *' to derived class pointer 'CustomQLabel *const' for 1st argument
      candidate function not viable: cannot convert from base class pointer 'QWidget *' to derived class pointer 'CustomQLabel *' for 1st argument
      candidate function not viable: cannot convert from base class pointer 'QWidget *' to 'const QVector<CustomQWidget *>' for 1st argument
      

      I've already tryed to static_cast the CustomQWidget to QWidget but nothing has changed.

      qv.append(static_cast<QWidget*>(widget));
      

      And I also tried with

      qv.push_back(widget);
      qv.insert(i, widget);
      

      Here's the CustomQWidget.h

      class CustomQWidget : public QWidget
      {
        Q_OBJECT
      public:
      ...
      private:
        Ui::CustomQWidget *ui;
      ...
      };
      

      I really don't know what to do, please help me

      JonBJ Online
      JonBJ Online
      JonB
      wrote on last edited by JonB
      #2

      @Emanuele-Papa
      Since you have QVector<CustomQWidget*> qv you can only append a CustomQWidget* to qv vector. ui->widgets_layout->itemAt(i)->widget() only returns (any kind of) QWidget*. You need to check that the widget is a CustomQWidget*:

              CustomQWidget*customWidget = qobject_cast<CustomQWidget*>(ui->widgets_layout->itemAt(i)->widget());
              if(customWidget != nullptr) {
                  qv.append(customWidget);
              }
      
      
      1 Reply Last reply
      4
      • E Offline
        E Offline
        Emanuele Papa
        wrote on last edited by
        #3

        It worked! Thank you

        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