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. Problem with spacers

Problem with spacers

Scheduled Pinned Locked Moved Solved General and Desktop
10 Posts 4 Posters 4.3k 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.
  • E Offline
    E Offline
    El3ctroGh0st
    wrote on last edited by El3ctroGh0st
    #1

    Hello everyone,

    I am at the moment working on a small toDo-app and am facing a problem with spacers. This is how it looks right now: However, once I start the app and add one of the tasks (in the code the tasks are being added to "tasksLayout") no tasks appear when I add some:

    Once I change the sizePolicy of "taskSpacer" to for example "Maximum", it does work as can be seen here:
    alt text
    This is not how I want it to look though. Does anyone know why it doesn't seem to work with "Expanding"?

    1 Reply Last reply
    0
    • M Offline
      M Offline
      medyakovvit
      wrote on last edited by
      #2

      Hi, @El3ctroGh0st
      Could you show the code where you add tasks to "tasksLayout"?

      E 1 Reply Last reply
      1
      • E Offline
        E Offline
        El3ctroGh0st
        wrote on last edited by
        #3
        This post is deleted!
        1 Reply Last reply
        0
        • M medyakovvit

          Hi, @El3ctroGh0st
          Could you show the code where you add tasks to "tasksLayout"?

          E Offline
          E Offline
          El3ctroGh0st
          wrote on last edited by
          #4

          @medyakovvit Thanks for answering! Sure thing, this is the code:

          void MainWindow::addTask()
          {
              bool ok;
              QString name = QInputDialog::getText(this,
                                                   tr("Add task"),
                                                   tr("Task name"),
                                                  QLineEdit::Normal,
                                                   tr("Untitled task"),
                                                   &ok);
              if (ok && !name.isEmpty())
              {
                  qDebug() << "Adding new task";
                  Task *task = new Task(name);
                  connect(task, &Task::removed, this, &MainWindow::removeTask);
                  connect(task, &Task::removed, this, &MainWindow::taskStatusChanged);
                  mTasks.append(task);
                  ui->tasksLayout->addWidget(task);
                  updateStatus();
              }
          }
          
          1 Reply Last reply
          0
          • VRoninV Offline
            VRoninV Offline
            VRonin
            wrote on last edited by
            #5

            what happens if you add task->setMinimumHeight(20);?

            In any case You pobably want to use QTableWidget instead of this

            "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
            ~Napoleon Bonaparte

            On a crusade to banish setIndexWidget() from the holy land of Qt

            E 1 Reply Last reply
            2
            • VRoninV VRonin

              what happens if you add task->setMinimumHeight(20);?

              In any case You pobably want to use QTableWidget instead of this

              E Offline
              E Offline
              El3ctroGh0st
              wrote on last edited by
              #6

              @VRonin It did change something!
              alt text
              However, it still seems to be buggy.

              1 Reply Last reply
              0
              • BuckwheatB Offline
                BuckwheatB Offline
                Buckwheat
                wrote on last edited by
                #7

                Hello!

                I see you are using addWidget. Have you thought of using insertWidget and placing it before the vertical spacer? This will keep everything towards the top. By adding, you are most likely going after the vertical spacer and it is off the dialog. Using layouts is pretty easy. Manually adding them in code is not as simple as letting designer do it for you because you do not have left, right, top, and bottom margins. You just have setMargin.

                I use multiple layouts to group controls. For instance, I would make a horizontal layout for the task and then add that to a vertical layout setting the spacing to something nice. You can also use a grid layout and just add to the rows.

                As stated in an erlier post, you can use a table widget. This has its own complications but looks nice. You can add a panel with scrolling as your task area and add layout items. This will scroll nicely in-between your header and buttons. And, you can hide it with one call!

                I also try to avoid using setMinimumHeight, etc. I prefer to let the layouts do there magic.

                Dave Fileccia

                E 1 Reply Last reply
                0
                • BuckwheatB Buckwheat

                  Hello!

                  I see you are using addWidget. Have you thought of using insertWidget and placing it before the vertical spacer? This will keep everything towards the top. By adding, you are most likely going after the vertical spacer and it is off the dialog. Using layouts is pretty easy. Manually adding them in code is not as simple as letting designer do it for you because you do not have left, right, top, and bottom margins. You just have setMargin.

                  I use multiple layouts to group controls. For instance, I would make a horizontal layout for the task and then add that to a vertical layout setting the spacing to something nice. You can also use a grid layout and just add to the rows.

                  As stated in an erlier post, you can use a table widget. This has its own complications but looks nice. You can add a panel with scrolling as your task area and add layout items. This will scroll nicely in-between your header and buttons. And, you can hide it with one call!

                  I also try to avoid using setMinimumHeight, etc. I prefer to let the layouts do there magic.

                  E Offline
                  E Offline
                  El3ctroGh0st
                  wrote on last edited by El3ctroGh0st
                  #8

                  @Buckwheat Thanks for answering. I decided to give QTableWidget a try (since insertWidget also did not work). If I got that right I first have to create a QTableWidgetItem and then add it to the QTableWidget.

                  QTableWidgetItem *newItem = new QTableWidgetItem(tr("%1").arg(
                      pow(row, column+1)));
                  tableWidget->setItem(row, column, newItem);
                  

                  However, I'm not quite sure about how to "convert" the Task widget to a QTableWidgetItem. Could you help me out there? This is what I tried (tableWidget is the QTableWidget I added to the GUI in the Designer):

                  void MainWindow::addTask()
                  {
                      bool ok;
                      QString name = QInputDialog::getText(this,
                                                           tr("Add task"),
                                                           tr("Task name"),
                                                          QLineEdit::Normal,
                                                           tr("Untitled task"),
                                                           &ok);
                      if (ok && !name.isEmpty())
                      {
                          qDebug() << "Adding new task";
                          Task *task = new Task(name);
                          connect(task, &Task::removed, this, &MainWindow::removeTask);
                          connect(task, &Task::removed, this, &MainWindow::taskStatusChanged);
                          mTasks.append(task);
                          QTableWidgetItem taskWidget = new QTableWidgetItem(task);
                          ui->tableWidget->insertRow(ui->tableWidget->rowCount);
                          ui->tableWidget->setItem(ui->tableWidget->rowCount()-1,0,taskWidget);
                          updateStatus();
                      }
                  }
                  
                  1 Reply Last reply
                  0
                  • BuckwheatB Offline
                    BuckwheatB Offline
                    Buckwheat
                    wrote on last edited by
                    #9

                    @El3ctroGh0st said in Problem with spacers:

                    widget to a QTableWidgetItem. Could you help me out there? Th

                    @El3ctroGh0st
                    I normally do not use table widgets for editing. You can do it with an cell widget. From what I saw, you would use setCellWidget for your buttons.

                    ui->tableWidget->setCellWidget (row, column, button);

                    Also, if you are going to use a QTableWidget, remove the vertical spacer and let your table fill the space. Hide your headers and set the task column to have be checkable and stretched.

                    I do not think you can directly convert Task to a table widget. You can, take task and return a stringlist with your values and create the QTableWidgetItems with it.

                    *** NORMAL DISCLAIMER --- Might be errors! ***

                    int row = ui->tableWidget->rowCount ();
                    int col = 0;
                    ui->tableWidget->insertRow (row);
                    for (auto& text : task->toList ()) {
                    QTableWidgetItem* item = new QTableWidgetItem (text);
                    if (col == 0) {
                    item->setFlags (Qt::ItemIsUserCheckable);
                    /* set check state based on data */
                    }
                    ui->tableWidget->setItem (row, col, item);
                    col++;
                    }
                    ui->tableWidget->setCellWidget (row, 1, new QPushButton (tr ("Edit")));
                    ui->tableWidget->setCellWidget (row, 2, new QPushButton (tr ("Remove")));

                    Or, make a method in your task that loads the table...
                    task->setTaskRow (ui->tableWidget, row);
                    that does all the work shown above.

                    If you feel really adventurous and want to learn some more advanced techniques (I am still learning these), you can create a model (ie. QFileSystemModel, etc) for your data and then the table widget, view, list view, tree view, etc. will automatically show your data, edit your data, etc and all you would have to do, if you wish, is to setup how the table will look style wise.

                    Dave Fileccia

                    E 1 Reply Last reply
                    1
                    • BuckwheatB Buckwheat

                      @El3ctroGh0st said in Problem with spacers:

                      widget to a QTableWidgetItem. Could you help me out there? Th

                      @El3ctroGh0st
                      I normally do not use table widgets for editing. You can do it with an cell widget. From what I saw, you would use setCellWidget for your buttons.

                      ui->tableWidget->setCellWidget (row, column, button);

                      Also, if you are going to use a QTableWidget, remove the vertical spacer and let your table fill the space. Hide your headers and set the task column to have be checkable and stretched.

                      I do not think you can directly convert Task to a table widget. You can, take task and return a stringlist with your values and create the QTableWidgetItems with it.

                      *** NORMAL DISCLAIMER --- Might be errors! ***

                      int row = ui->tableWidget->rowCount ();
                      int col = 0;
                      ui->tableWidget->insertRow (row);
                      for (auto& text : task->toList ()) {
                      QTableWidgetItem* item = new QTableWidgetItem (text);
                      if (col == 0) {
                      item->setFlags (Qt::ItemIsUserCheckable);
                      /* set check state based on data */
                      }
                      ui->tableWidget->setItem (row, col, item);
                      col++;
                      }
                      ui->tableWidget->setCellWidget (row, 1, new QPushButton (tr ("Edit")));
                      ui->tableWidget->setCellWidget (row, 2, new QPushButton (tr ("Remove")));

                      Or, make a method in your task that loads the table...
                      task->setTaskRow (ui->tableWidget, row);
                      that does all the work shown above.

                      If you feel really adventurous and want to learn some more advanced techniques (I am still learning these), you can create a model (ie. QFileSystemModel, etc) for your data and then the table widget, view, list view, tree view, etc. will automatically show your data, edit your data, etc and all you would have to do, if you wish, is to setup how the table will look style wise.

                      E Offline
                      E Offline
                      El3ctroGh0st
                      wrote on last edited by
                      #10

                      @Buckwheat Sorry, I was on vacation. Thanks fo your reply!

                      1 Reply Last reply
                      0

                      • Login

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