0

I am looking for an easy way to link hand coded design to the UI for widgets applications in Qt. I plan to use the UI builder to easily adjust the layouts and obtain proper spacing, which I find hard to do without the UI builder.
I want to create a 3x3 grid of buttons for which I plan to use QVector< QVector<QPushButton*> > (I am not sure how I would do this in UI builder.)

Here is what I have tried, Why are the buttons not displayed even when I set the parent of each button to the widget ?

main.cpp

#include "window.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Window w;
    w.show();

    return a.exec();
}

window.h

#ifndef WINDOW_H
#define WINDOW_H

#include <QWidget>
#include <QPushButton>

namespace Ui {
class Window;
}

class Window : public QWidget
{
    Q_OBJECT

public:
    explicit Window(QWidget *parent = 0);
    ~Window();

private:
    Ui::Window *ui;
    static const int tiles = 50, height = 600, width = 500;
    QVector< QVector<QPushButton*> > cells;
};

#endif // WINDOW_H

window.cpp

#include "window.h"
#include "ui_window.h"

Window::Window(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Window)
{
    ui->setupUi(this);
    this->resize(width, height);

    int j = 0;
    for(auto &row : cells)
    {
        int i = 0;
        for(auto &col : row)
        {
            col = new QPushButton(this);
            col->setGeometry(i, j, tiles, tiles);
            i += tiles;
        }

        j += tiles;
    }
}

Window::~Window()
{
    delete ui;
    for(auto &row : cells)
    {
        for(auto &col : row)
        {
            delete col;
        }

    }
}

Any help would be appreciated.

CaptainDaVinci
  • 975
  • 7
  • 23

1 Answers1

2

The vectors are empty, so you iterate over nothing. Instead of managing these manually, you could leverage the grid layout.

Alas, you're complicating things unnecessarily with all the manual memory management and geometry management. It's unnecessary. All you need to do is to add widgets to the layout you allude to. And even then, I don't see how relegating the layout to a .ui file helps you since there the layout must be empty. So yes: you can set spacings, but you won't see them until you run the code. So it seems like a pointless exercise, unless you have other elements you're not telling us about (why aren't you - you went so far already).

Below is a minimum example that simplifies it as much as practicable, but see this answer and the links therein for more idea.

// https://github.com/KubaO/stackoverflown/tree/master/questions/button-grid-43214317
#include <QtWidgets>

namespace Ui { class Window {
public:
   // Approximate uic output
   QGridLayout *layout;
   void setupUi(QWidget * widget) {
      layout = new QGridLayout(widget);
   }
}; }

class Window : public QWidget
{
   Q_OBJECT
   Ui::Window ui;
   QPushButton * buttonAt(int row, int column) {
      auto item = ui.layout->itemAtPosition(row, column);
      return item ? qobject_cast<QPushButton*>(item->widget()) : nullptr;
   }

public:
   explicit Window(QWidget *parent = {});
};

Window::Window(QWidget *parent) : QWidget(parent) {
   ui.setupUi(this);
   for (int i = 0; i < 5; ++i)
      for (int j = 0; j < 6; ++j)
      {
         auto b = new QPushButton(QStringLiteral("%1,%2").arg(i).arg(j));
         ui.layout->addWidget(b, i, j);
      }
}

int main(int argc, char *argv[])
{
   QApplication a(argc, argv);
   Window w;
   w.show();
   return a.exec();
}
#include "main.moc"
Community
  • 1
  • 1
Kuba hasn't forgotten Monica
  • 95,931
  • 16
  • 151
  • 313