Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Create a vector of QPushButtons?



  • I want to create a list of buttons, each with unique names and colors. I believe that a vector would be good for this, since I can just push back objects "for ever", without the space limit of arrays. My code is

    QPushButton *crt = new QPushButton(ui->frame_with_btns);
    
            crt->setText(QString("text"));
            crt->move(0, 30*total_btn);
            crt->resize(186, 30);
            crt->setStyleSheet("QPushButton{background-color:red;}");
            crt->topLevelWidget();
            crt->show();
            v_btn.push_back(*crt);
            total_btn += 1;
    

    and I've defined a vector in my .h file as

    std::vector<QPushButton> v_btn;
    int total_btn = 0;
    

    which gives me the error

    error: call to deleted constructor of 'QPushButton' ::new((void*)__p) _Up(_VSTD::forward<_Args>(__args)...);
    


  • @legitnameyo
    I'm not a C++-er. But should you not be creating a vector of the pointers to the push buttons you are creating?

    std::vector<QPushButton *> v_btn;
    ...
    v_btn.push_back(crt);
    

    What you're trying to do will try to copy the QPushButtons, which Qt does not allow.

    Is that right?



  • that solved it! Thanks!


Log in to reply