Results 1 to 20 of 21

Thread: How to shrink a QMainWindow when its central widget is shrinked

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2016
    Posts
    76
    Thanks
    28
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Windows Android

    Default How to shrink a QMainWindow when its central widget is shrinked

    Hi all,

    Please take a look at these files:

    List.h:

    Qt Code:
    1. #ifndef LIST_H
    2. #define LIST_H
    3.  
    4. #include <QMainWindow>
    5. #include "task.h"
    6. #include <QVector>
    7.  
    8. class QLabel;
    9.  
    10. class List : public QMainWindow
    11. {
    12. Q_OBJECT
    13.  
    14. public:
    15. explicit List(QWidget *parent = nullptr);
    16. void updateStatus();
    17. ~List();
    18.  
    19. public slots:
    20. void addTask();
    21. void removeTask(Task*);
    22. void taskStatusChanged();
    23.  
    24. private:
    25. QVector<Task*> mTasks;
    26. QLabel* statusLabel;
    27. QPushButton* addTaskButtun;
    28. QHBoxLayout* hBox;
    29. QVBoxLayout* vBox;
    30. };
    31.  
    32. #endif // LIST_H
    To copy to clipboard, switch view to plain text mode 

    List.cpp:

    Qt Code:
    1. #include "list.h"
    2. #include <QPushButton>
    3. #include <QLabel>
    4. #include <QHBoxLayout>
    5. #include <QVBoxLayout>
    6. #include <QString>
    7. #include <QInputDialog>
    8. #include <QLineEdit>
    9.  
    10. List::List(QWidget *parent)
    11. : QMainWindow(parent), mTasks()
    12. {
    13. statusLabel = new QLabel(tr("Status: 0 todo / 0 done"));
    14. addTaskButtun = new QPushButton(tr("Add task"));
    15.  
    16. hBox = new QHBoxLayout;
    17. hBox->addWidget(statusLabel);
    18. hBox->addStretch();
    19. hBox->addWidget(addTaskButtun);
    20.  
    21. vBox = new QVBoxLayout;
    22. vBox->addLayout(hBox);
    23. vBox->addStretch();
    24.  
    25. QWidget *widget = new QWidget;
    26. widget->setLayout(vBox);
    27. setCentralWidget(widget);
    28.  
    29. connect(addTaskButtun, &QPushButton::clicked, this, &List::addTask);
    30. updateStatus();
    31. }
    32.  
    33. //************************************************
    34.  
    35. void List::addTask() {
    36. bool ok;
    37. QString name = QInputDialog::getText(this, tr("Add Task"), tr("Task name"),
    38. QLineEdit::Normal, tr("Untitled task"), &ok);
    39.  
    40. if(ok && !name.isEmpty()) {
    41. Task* task = new Task(name);
    42. connect(task, &Task::removed, this, &List::removeTask);
    43. connect(task, &Task::statusChanged, this, &List::taskStatusChanged);
    44. mTasks.append(task);
    45. vBox->addWidget(task);
    46. updateStatus();
    47. }
    48. }
    49.  
    50. //*********************************************
    51.  
    52. void List::removeTask(Task* task) {
    53. mTasks.removeOne(task);
    54. vBox->removeWidget(task);
    55. delete task;
    56. updateStatus();
    57. }
    58.  
    59. //**************************************
    60.  
    61. void List::taskStatusChanged() {
    62. updateStatus();
    63. }
    64.  
    65. //************************************
    66.  
    67. void List::updateStatus() {
    68. int completedCount = 0;
    69.  
    70. for(auto t: mTasks)
    71. if(t->isCompleted())
    72. completedCount++;
    73.  
    74. int todoCount = mTasks.size() - completedCount;
    75. statusLabel->setText(QString("Statuse: %1 todo / %2 completed")
    76. .arg(todoCount) .arg(completedCount));
    77. }
    78.  
    79. //*****************************
    80.  
    81. List::~List()
    82. {
    83. delete vBox;
    84. }
    To copy to clipboard, switch view to plain text mode 

    Task.h:

    Qt Code:
    1. #ifndef TASK_H
    2. #define TASK_H
    3.  
    4. #include <QWidget>
    5. #include <QString>
    6.  
    7. class QCheckBox;
    8.  
    9. class Task : public QWidget
    10. {
    11. Q_OBJECT
    12.  
    13. public:
    14. explicit Task(const QString&, QWidget* parent = nullptr);
    15.  
    16. void setName(const QString&);
    17. QString name() const;
    18. bool isCompleted() const;
    19.  
    20. ~Task();
    21.  
    22. public slots:
    23. void rename();
    24.  
    25. signals:
    26. void removed(Task*);
    27. void statusChanged(Task*);
    28.  
    29. private slots:
    30. void checked(bool);
    31.  
    32. private:
    33. QCheckBox* checkbox;
    34. QPushButton* editButton;
    35. QPushButton* removeButton;
    36. QHBoxLayout* hBox;
    37. };
    38.  
    39. #endif // TASK_H
    To copy to clipboard, switch view to plain text mode 


    Task.cpp:

    Qt Code:
    1. #include "task.h"
    2. #include "list.h"
    3. #include <QInputDialog>
    4. #include <QWidget>
    5. #include <QHBoxLayout>
    6. #include <QPushButton>
    7. #include <QCheckBox>
    8.  
    9. Task::Task(const QString& name, QWidget* parent) : QWidget (parent)
    10. {
    11. checkbox = new QCheckBox;
    12. editButton = new QPushButton(tr("Edit"));
    13. removeButton = new QPushButton(tr("Remove"));
    14.  
    15. hBox = new QHBoxLayout;
    16. hBox->addWidget(checkbox);
    17. hBox->addStretch();
    18. hBox->addWidget(editButton);
    19. hBox->addWidget(removeButton);
    20.  
    21. setLayout(hBox);
    22.  
    23. setName(name);
    24.  
    25. connect(editButton, &QPushButton::clicked, this, &Task::rename);
    26. connect(removeButton, &QPushButton::clicked, [this]()->void {
    27. emit removed(this);
    28. });
    29. connect(checkbox, &QCheckBox::toggled, this, &Task::checked);
    30. };
    31.  
    32. //*************************************
    33.  
    34. void Task::setName(const QString& name) {
    35. checkbox->setText(name);
    36. }
    37.  
    38. //********************************************
    39.  
    40. QString Task::name() const {
    41. return checkbox->text();
    42. }
    43.  
    44. //*****************************
    45.  
    46. bool Task::isCompleted() const {
    47. return checkbox->isChecked();
    48. }
    49.  
    50. //****************************************
    51.  
    52. void Task::rename() {
    53. bool ok;
    54. QString value = QInputDialog::getText(this, tr("Edit task"), tr("Task name"),
    55. QLineEdit::Normal, this->name(), &ok);
    56. if(ok && !value.isEmpty())
    57. setName(value);
    58. }
    59.  
    60. //************************************
    61.  
    62. void Task::checked(bool checked) {
    63. QFont font(checkbox->font());
    64. font.setStrikeOut(checked);
    65. checkbox->setFont(font);
    66.  
    67. emit statusChanged(this);
    68. }
    69.  
    70. //**************************************************
    71.  
    72. Task::~Task() {
    73. delete hBox;
    74. }
    To copy to clipboard, switch view to plain text mode 

    When I add tasks "untitled task 1" to "untitled task 3", image number 1 below:

    3.jpg

    then remove "untitled task 2", the frame/window doesn't shrink and the space for that task is just vacant, image number 2. I like it to decrease and fit the remained tasks, like image number 3. When a task is added, the window increases in size, so when one is removed, it should decrease in size too.

    I tested the followings one by one in void List::updateStatus() {, but none worked as expected!

    What should be used instead, please?

    Qt Code:
    1. this->sizeHint();
    2. this->update();
    3. this->resize(sizeHint());
    4. this->adjustSize();
    To copy to clipboard, switch view to plain text mode 
    Last edited by franky; 21st July 2019 at 17:25.

Similar Threads

  1. Replies: 1
    Last Post: 10th August 2014, 21:06
  2. Replies: 0
    Last Post: 28th March 2013, 00:22
  3. Widget auto shrink
    By been_1990 in forum Qt Programming
    Replies: 9
    Last Post: 24th January 2013, 01:43
  4. Replies: 8
    Last Post: 28th June 2011, 14:57
  5. Central Widget of QMainWindow
    By sumsin in forum Qt Programming
    Replies: 3
    Last Post: 13th March 2006, 18:32

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.