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. [solved] foreach (QPushButton, QVBoxLayout) question ?
Forum Update on Monday, May 27th 2025

[solved] foreach (QPushButton, QVBoxLayout) question ?

Scheduled Pinned Locked Moved General and Desktop
4 Posts 2 Posters 2.8k 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.
  • D Offline
    D Offline
    deleted28
    wrote on 13 Mar 2014, 09:33 last edited by
    #1

    The following code adds Pushbuttons to QVBoxLayout:
    @void MainWindow::on_pb_test_1_clicked() {

    vlw_1 = new QWidget(this);
    vlw_1->setObjectName(QString::fromUtf8("vlW_1"));
    vlw_1->setGeometry(QRect(10, 10, 40, 200));
    
    vl_1 = new QVBoxLayout(vlw_1);
    vl_1->setSpacing(6);
    vl_1->setContentsMargins(11, 11, 11, 11);
    vl_1->setObjectName(QString::fromUtf8("vl_1"));
    vl_1->setContentsMargins(0, 0, 0, 0);
    
    for (int i=0; i<5;i++) {
        pb_x = new QPushButton(vlw_1);
        pb_x->setObjectName(QString("pb_x%1").arg(i));
        pb_x->setText(QString("x%1").arg(i));
        pb_x->setCheckable(true);
        vl_1->addWidget(pb_x);
    }
    
    vlw_1->setStyleSheet("QPushButton:checked{color: green; background-color: red;}");
    vlw_1->show();
    

    }@

    in another function i want to access all this Pushbuttons in this QVBoxLayout using 'foreach'

    @void MainWindow::on_pb_test_2_clicked() {

    qDebug() << vl_1->count();
    qDebug() << vlw_1->children().count();
    
    foreach(QPushButton *x, ??? ) {
    
        // do something on each Pushbutton
    }@
    

    By what should be "???" substituted ?

    1 Reply Last reply
    0
    • S Offline
      S Offline
      sierdzio
      Moderators
      wrote on 13 Mar 2014, 12:05 last edited by
      #2

      Nothing: it will not work with QLayout.

      But you can use "QObject::findChildren()":http://qt-project.org/doc/qt-5/qobject.html#findChildren to get all QPushButtons and iterate over the list that is returned.

      (Z(:^

      1 Reply Last reply
      0
      • D Offline
        D Offline
        deleted28
        wrote on 13 Mar 2014, 13:02 last edited by
        #3

        @void MainWindow::on_pb_test_2_clicked() {

        QList<QPushButton*> bList = vlw_1->findChildren<QPushButton*>();
        
        foreach(QPushButton *x, bList) 
        {
            qDebug() << x->objectName() ;
        }
        

        }@
        works nice on the QWidget vlw_1. Maybe my fist foreach approach works on QWidget ?

        1 Reply Last reply
        0
        • S Offline
          S Offline
          sierdzio
          Moderators
          wrote on 13 Mar 2014, 13:06 last edited by
          #4

          "foreach" works on container classes, that is:

          • QList
          • QStringList
          • QMap
          • QHash
          • QVector
          • probably some more (QQueue, QMultiHash, etc.)

          it does not work on QWidget or QLayout by itself. But when you have - for example - a list of widgets, then it is a different story (and a happy one: see the solution that works for you :)).

          (Z(:^

          1 Reply Last reply
          0

          1/4

          13 Mar 2014, 09:33

          • Login

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