How to append CustomQWidget from a QVBoxLayout to QVector
-
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 argumentI've already tryed to
static_castthe 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
-
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 argumentI've already tryed to
static_castthe 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
@Emanuele-Papa
Since you haveQVector<CustomQWidget*> qvyou can only append aCustomQWidget*toqvvector.ui->widgets_layout->itemAt(i)->widget()only returns (any kind of)QWidget*. You need to check that the widget is aCustomQWidget*:CustomQWidget*customWidget = qobject_cast<CustomQWidget*>(ui->widgets_layout->itemAt(i)->widget()); if(customWidget != nullptr) { qv.append(customWidget); } -
It worked! Thank you