Results 1 to 2 of 2

Thread: How do i update this grid of buttons?

  1. #1
    Join Date
    May 2018
    Posts
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default How do i update this grid of buttons?

    I have an n and m as two global variables and I wish to generate a grid of buttons size n*m. I have a combo box which changes n and m if the option is changed. My question is how do I update the grid?

    Here is my code:

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include "QPushButton"
    #include "QGridLayout"
    #include "QLabel"
    #include "QComboBox"
    #include "QDebug"

    int n=9,m=9;

    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    QWidget *centralWidget = new QWidget;
    int count=1,i,j;

    QPushButton *button[10][10];
    QGridLayout *controlsLayout = new QGridLayout;
    QGridLayout *controlsLayout1 = new QGridLayout;
    QPushButton *Reset=new QPushButton;
    QLabel *Bombs=new QLabel;
    QLabel *Timer=new QLabel;

    Timer->setText("timer");
    Bombs->setText("bombs");

    QStringList commands = { "Easy", "Medium", "Hard", "Custom" };
    QComboBox* combo = new QComboBox(this);
    combo->addItems(commands);
    connect(combo, SIGNAL(currentIndexChanged(int)), this, SLOT(commandChanged(int)));

    controlsLayout->addWidget(combo,0,0,1,2);
    controlsLayout->addWidget(Bombs,1,0);
    controlsLayout->addWidget(Reset,1,4);
    controlsLayout->addWidget(Timer,1,8);

    Reset->setMinimumSize(40,40);
    Reset->setMaximumSize(40,40);

    for(i=2;i<n;i++)
    {
    for(j=0;j<m;j++)
    {
    if(count<=100)
    {

    button[i][j] = new QPushButton();
    button[i][j]->setMinimumSize(40,40);
    button[i][j]->setMaximumSize(40,40);
    button[i][j]->move(40*j, 40*i);
    button[i][j]->show();
    controlsLayout->addWidget(button[i][j], i, j);
    count++;
    }
    }
    }
    controlsLayout->setSpacing(5);
    centralWidget->setLayout(controlsLayout);
    setCentralWidget(centralWidget);
    }
    void MainWindow::commandChanged(int index)
    {
    if (index==0){n=9;m=9;}
    else{n=9;m=9;}

    qDebug() << n<<" "<<m;

    }
    MainWindow::~MainWindow()
    {
    delete ui;
    }

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How do i update this grid of buttons?

    You start by moving almost all of the code you have in the MainWindow constructor into a separate method, something like "buildButtonGrid( int n, int m )". Then you rewrite the code to get rid of all of the hard-coded constants (like "10", "100", and so forth) and replace them with "n", "m", "n + 1", "m + 1", etc. as appropriate. You also get rid of the hard-coded "QPushButton * button[10][10]" and replace it with a class member variable "QVector< QButton * > buttons" or something similar that you resize as needed to hold n*m button pointers.

    Finally, you get rid of the absolute positioning ("move( 40 * j, 40 * i)") because the grid control will put the buttons in the necessary places.

    When you resize the grid, be sure you also remember to delete the previous buttons.

    This piece of code shows that you have a logical error in your thinking, because it looks like you had a problem with indexing into your button array and don't understand why. That's why you had to add the "count" variable. You need to rethink this.
    Qt Code:
    1. for(i=2;i<n;i++)
    2. {
    3. for(j=0;j<m;j++)
    4. {
    5. if(count<=100)
    6. {
    7.  
    8. button[i][j] = new QPushButton();
    9. button[i][j]->setMinimumSize(40,40);
    10. button[i][j]->setMaximumSize(40,40);
    11. button[i][j]->move(40*j, 40*i);
    12. button[i][j]->show();
    13. controlsLayout->addWidget(button[i][j], i, j);
    14. count++;
    15. }
    16. }
    17. }
    To copy to clipboard, switch view to plain text mode 
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. Replies: 8
    Last Post: 5th May 2021, 17:41
  2. Replies: 2
    Last Post: 4th September 2014, 12:09
  3. Replies: 0
    Last Post: 1st February 2014, 17:35
  4. Grid Layout -- and buttons spacing
    By Noob in forum Newbie
    Replies: 4
    Last Post: 6th May 2013, 08:30
  5. Replies: 14
    Last Post: 7th December 2011, 04:57

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.