Results 1 to 10 of 10

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

Hybrid View

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

    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.

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

    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...

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

    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.

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

    aurora (2nd November 2011)

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

    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.

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

    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
  •  
Qt is a trademark of The Qt Company.