Results 1 to 10 of 10

Thread: how to delete dynamically created widget, when "clear" button pressed?

  1. #1
    Join Date
    Oct 2011
    Posts
    160
    Thanks
    31
    Qt products
    Qt4
    Platforms
    Windows

    Default how to delete dynamically created widget, when "clear" button pressed?

    This code will create line box and a combox when clicked on a "ADD" button, but at the same time i would like to provide "CLEAR "button, which will delete all these creations...how can i able to do that?

    Qt Code:
    1. void fileopen::on_pushButton_ADD_clicked()
    2. {
    3.  
    4. static int LayoutCount;
    5.  
    6. QLineEdit *lineEdit = new QLineEdit;
    7.  
    8.  
    9. QLabel *label = new QLabel;
    10. QLabel *label2 =new QLabel;
    11. QComboBox *comboBox =new QComboBox;
    12. comboBox->addItem("COUNT");
    13. label->setText("ENTER SEARCH KEY");
    14. label2->setText("UNDER THE TAG");
    15.  
    16. ui->gridLayout_3->alignment();
    17. ui->gridLayout->addWidget( label,LayoutCount,0 );
    18. ui->gridLayout->addWidget( lineEdit,LayoutCount,1 );
    19. ui->gridLayout->addWidget( label2,LayoutCount,2 );
    20. ui->gridLayout->addWidget(comboBox,LayoutCount,3);
    21.  
    22. lineEditList.append(lineEdit);
    23. TagList.append(comboBox);
    24.  
    25. LayoutCount=LayoutCount+1;
    26.  
    27. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Oct 2011
    Posts
    9
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: how to delete dynamically created widget, when "clear" button pressed?

    hello

    hm maybe one possible solution is, to create a "QWidget-Field" in your Class and add all Widgets u create in this Field and u need to have some pointer to move through the list to delete the Widgets.
    e.g:
    Qt Code:
    1. class myClass{
    2. ...
    3. private:
    4. QWidget Field[20]; // can have 20 Widgets
    5. QWidget* fieldStart; // temp pointer to sace the startadress
    6. QWidget* posWidget; // has the actual position of the last widget
    7. QWidget* posToDelete;
    8. }
    To copy to clipboard, switch view to plain text mode 

    and in your implementation:

    Qt Code:
    1. ...
    2. void fileOpen::AddWidget{
    3. ...
    4. QPushButton* button = new QPushButton();
    5. Field[0] = *button;
    6. posWidget++;
    7. ...
    8. }
    To copy to clipboard, switch view to plain text mode 
    Maybe there is also a Widget/Class in Qt that have the described behavior

    i hope i could inspire you

    with best regards

    nudels

  3. The following user says thank you to nudels for this useful post:

    aurora (14th December 2011)

  4. #3
    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 to delete dynamically created widget, when "clear" button pressed?

    This code will create line box and a combox when clicked on a "ADD" button, but at the same time i would like to provide "CLEAR "button, which will delete all these creations...how can i able to do that?
    That would be a very strange design for a user interface. Are you sure you want to do that, and not just show or hide the widgets when you click the two buttons?

  5. #4
    Join Date
    Feb 2010
    Location
    Sydney, Australia
    Posts
    111
    Thanks
    18
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: how to delete dynamically created widget, when "clear" button pressed?

    I agree with d_stranz. Create a composite widget that contains those editing widgets you require and then enable or unhide that composite widget when ADD is clicked. You could connect a Clear() slot on the composite widget to a clear() signal (e.g. clicked() from the clear button) that would call clear() on the individual widgets and then also hide the composite widget.

    If you absolutely wanted to do it your way, you could connect the clear() signal (e.g. clicked() from the clear button) to deleteLater() on each of the created UI elements, but I REALLY don't recommend doing it that way. It doesn't seem to be the Qt way.

    Also, be careful with the parenting of QObjects/QWidgets when you create them on the heap. It's best to parent the objects explicitly, e.g. QWidget* myWidget = new QWidget(someValidParent). You're passing them into addWidget functions in the case above, so the parenting should be sorted out properly. But memory management can become a problem if you don't ensure that QObjects/QWidgets are properly parented.

  6. #5
    Join Date
    Sep 2011
    Location
    Manchester
    Posts
    538
    Thanks
    3
    Thanked 106 Times in 103 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: how to delete dynamically created widget, when "clear" button pressed?

    Show/Hide approach won't work when number of widgets to show is unknown/changing.
    It's a common approach in this case to create new elements rather than show part of predefined set.

    Quote Originally Posted by stefanadelbert View Post
    you could connect the clear() signal (e.g. clicked() from the clear button) to deleteLater() on each of the created UI elements
    Imho that's perfectly fine and that's how I would do it. Only thing to remember here is NOT to keep any pointers to those objects.

  7. #6
    Join Date
    Oct 2011
    Posts
    160
    Thanks
    31
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: how to delete dynamically created widget, when "clear" button pressed?

    Guys....would u mind to tell what i need to do now please ? i'm confused.....
    without pointer means...i think its impossible...

  8. #7
    Join Date
    Sep 2011
    Location
    Manchester
    Posts
    538
    Thanks
    3
    Thanked 106 Times in 103 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: how to delete dynamically created widget, when "clear" button pressed?

    Try something like that:

    mainwindow.h:
    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include <QtGui/QMainWindow>
    5.  
    6.  
    7. class MainWindow : public QMainWindow
    8. {
    9. Q_OBJECT
    10.  
    11. public slots:
    12. void addRow( void );
    13.  
    14. public:
    15. MainWindow(QWidget *parent = 0);
    16. ~MainWindow();
    17.  
    18. private:
    19. QVBoxLayout* m_layout;
    20. };
    21.  
    22. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 
    mainwindow.cpp
    Qt Code:
    1. #include "mainwindow.h"
    2.  
    3. #include <QVBoxLayout>
    4. #include <QHBoxLayout>
    5. #include <QPushButton>
    6. #include <QLineEdit>
    7.  
    8. MainWindow::MainWindow(QWidget *parent)
    9. : QMainWindow(parent),
    10. m_layout( new QVBoxLayout() )
    11. {
    12. QPushButton* add = new QPushButton("Add");
    13. connect(add, SIGNAL(clicked()), this, SLOT(addRow()));
    14.  
    15. m_layout->addWidget(add);
    16. m_layout->setAlignment(Qt::AlignTop);
    17.  
    18. QWidget* w = new QWidget();
    19. w->setLayout(m_layout);
    20.  
    21. this->setCentralWidget(w);
    22. }
    23.  
    24. MainWindow::~MainWindow()
    25. {
    26.  
    27. }
    28.  
    29. void MainWindow::addRow( void )
    30. {
    31. QLineEdit* line = new QLineEdit();
    32. QPushButton* clear = new QPushButton("Clear");
    33. QPushButton* remove = new QPushButton("Remove");
    34.  
    35. QHBoxLayout* layout = new QHBoxLayout();
    36. layout->setMargin(0);
    37. layout->addWidget(line);
    38. layout->addWidget(clear);
    39. layout->addWidget(remove);
    40.  
    41. QWidget* w = new QWidget();
    42. w->setLayout(layout);
    43.  
    44. connect(clear, SIGNAL(clicked()), line, SLOT(clear()));
    45. connect(remove, SIGNAL(clicked()), w, SLOT(deleteLater()));
    46.  
    47. m_layout->addWidget(w);
    48. }
    To copy to clipboard, switch view to plain text mode 
    It should work exacly as you want it.

  9. The following user says thank you to Spitfire for this useful post:

    aurora (2nd November 2011)

  10. #8
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: how to delete dynamically created widget, when "clear" button pressed?

    Check this out
    Qt Code:
    1. //main.cpp
    2. #include <QtGui>
    3. #include "MyWizard.h"
    4.  
    5.  
    6. class MyWidget : public QWidget
    7. {
    8. Q_OBJECT
    9. public:
    10. MyWidget(QWidget* parent = 0)
    11. : QWidget(parent)
    12. , layout(new QGridLayout(this))
    13. , add(new QPushButton("Add Widgets", this))
    14. {
    15. layout->addWidget(add, layout->rowCount(), 0);
    16. connect(add, SIGNAL(clicked()), this, SLOT(addWidgets()));
    17. }
    18. private slots:
    19. void addWidgets()
    20. {
    21. QPushButton* clear = new QPushButton("Clear Widgets", this);
    22. QLineEdit* lineEdit = new QLineEdit;
    23. QLabel* label = new QLabel("ENTER SEARCH KEY");
    24. QLabel* label2 = new QLabel("UNDER THE TAG");
    25. QComboBox* comboBox = new QComboBox;
    26.  
    27.  
    28. layout->addWidget(clear, layout->rowCount(), 0);
    29. layout->addWidget(label, layout->rowCount() - 1, 1);
    30. layout->addWidget(lineEdit,layout->rowCount() - 1, 2);
    31. layout->addWidget(label2, layout->rowCount() - 1, 3);
    32. layout->addWidget(comboBox,layout->rowCount() - 1, 4);
    33.  
    34.  
    35. connect(clear, SIGNAL(clicked()), label, SLOT(deleteLater()));
    36. connect(clear, SIGNAL(clicked()), lineEdit, SLOT(deleteLater()));
    37. connect(clear, SIGNAL(clicked()), label2, SLOT(deleteLater()));
    38. connect(clear, SIGNAL(clicked()), comboBox, SLOT(deleteLater()));
    39. connect(clear, SIGNAL(clicked()), clear, SLOT(deleteLater()));
    40. }
    41. private:
    42. QGridLayout* layout;
    43. };
    44.  
    45.  
    46.  
    47.  
    48. int main(int argc, char *argv[])
    49. {
    50. QApplication a(argc, argv);
    51. MyWidget w;
    52. w.show();
    53.  
    54.  
    55. return a.exec();
    56. }
    57.  
    58.  
    59. #include "main.moc"
    To copy to clipboard, switch view to plain text mode 

  11. #9
    Join Date
    Oct 2011
    Posts
    160
    Thanks
    31
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: how to delete dynamically created widget, when "clear" button pressed?

    thank u so much.....but i wanted to include that form inside another form which is designed using "FORM DESIGNER".....
    SO I changed it as follows.....but i'm getting error!!! please help me to know wher i'm going wrong...

    Qt Code:
    1. MainWindow::MainWindow(QWidget *parent)
    2. : QMainWindow(parent),
    3. ui(new Ui::MainWindow),
    4. m_layout( new QVBoxLayout() )
    5. {
    6. ui->setupUi(this);
    7. QPushButton* add = new QPushButton("Add");
    8. connect(add, SIGNAL(clicked()), this, SLOT(addRow()));
    9.  
    10. m_layout->addWidget(add);
    11. .............
    12. .............
    To copy to clipboard, switch view to plain text mode 




    After compiling, i got error......All errors are respect to my declaration..."MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent),
    ui(new Ui::MainWindow),"

    error: class 'MainWindow' does not have any field named 'ui'
    error: invalid use of incomplete type 'struct Ui::MainWindow'
    error: forward declaration of 'struct Ui::MainWindow'
    error: 'ui' was not declared in this scope
    Last edited by aurora; 2nd November 2011 at 04:13.

  12. #10
    Join Date
    Sep 2011
    Location
    Manchester
    Posts
    538
    Thanks
    3
    Thanked 106 Times in 103 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: how to delete dynamically created widget, when "clear" button pressed?

    You're probably missing
    Qt Code:
    1. #include "ui_mainwindow.h"
    To copy to clipboard, switch view to plain text mode 
    .

Similar Threads

  1. Replies: 3
    Last Post: 8th December 2011, 19:21
  2. Replies: 4
    Last Post: 20th July 2011, 11:37
  3. "Change widget class on button click" problem
    By utkozanenje in forum Newbie
    Replies: 3
    Last Post: 23rd May 2011, 00:40
  4. How to "hide window when close button pressed"
    By iGoo in forum Qt Programming
    Replies: 4
    Last Post: 26th July 2006, 09:53

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.