Problem with spacers
-
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:
This is not how I want it to look though. Does anyone know why it doesn't seem to work with "Expanding"? -
Hi, @El3ctroGh0st
Could you show the code where you add tasks to "tasksLayout"? -
This post is deleted!
-
@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(); } }
-
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.
-
@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(); } }
-
@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.
-
@Buckwheat Sorry, I was on vacation. Thanks fo your reply!