Results 1 to 5 of 5

Thread: hiding textbox and other widgets when loading a form

  1. #1
    Join Date
    May 2012
    Posts
    33
    Thanks
    9
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default hiding textbox and other widgets when loading a form

    In my project i have a form, when i load it i want to hide textboxes and labels in it. When i click on a button then it should be visible...How to do it...?thank you

  2. #2
    Join Date
    May 2012
    Posts
    136
    Thanks
    2
    Thanked 27 Times in 24 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: hiding textbox and other widgets when loading a form

    The easyest way is to place the items in a widget, then hide the widget at the creation of the form.
    connect a self made slot to the clicked signal of the button and show the widget

    here is an example: Hide.zip
    Last edited by StrikeByte; 1st June 2012 at 10:09.

  3. #3
    Join Date
    May 2012
    Posts
    33
    Thanks
    9
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: hiding textbox and other widgets when loading a form

    @StrikeByte
    Can you please provide using some snippet. How to place items in a widget and how hide while creation?
    Os-> centos6
    Qt 4.7
    tool->Qcreator

  4. #4
    Join Date
    May 2012
    Posts
    136
    Thanks
    2
    Thanked 27 Times in 24 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: hiding textbox and other widgets when loading a form

    ok some code

    the .h file
    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include <QMainWindow>
    5. #include <QLabel>
    6. #include <QLayout>
    7. #include <QPushButton>
    8.  
    9. namespace Ui {
    10. class MainWindow;
    11. }
    12.  
    13. class MainWindow : public QMainWindow
    14. {
    15. Q_OBJECT
    16.  
    17. public:
    18. explicit MainWindow(QWidget *parent = 0);
    19. ~MainWindow();
    20.  
    21. private:
    22. QLabel *label1;
    23. QLabel *label2;
    24. QLabel *label3;
    25. QWidget *container;
    26. QGridLayout *layout;
    27. QVBoxLayout *containerLayout;
    28. QPushButton *btnShow;
    29. QWidget *centralWidget;
    30.  
    31. private slots:
    32. void showLabels();
    33. };
    34.  
    35. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 

    the .cpp file
    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3.  
    4. MainWindow::MainWindow(QWidget *parent) :
    5. QMainWindow(parent)
    6. {
    7. resize(800,600);
    8. centralWidget = new QWidget(this); //create the central widget
    9.  
    10. layout = new QGridLayout(centralWidget); //create mainwindow layout
    11. layout->setSpacing(6);
    12. layout->setContentsMargins(11, 11, 11, 11);
    13.  
    14. btnShow = new QPushButton("show/hide",centralWidget); //create the button
    15. layout->addWidget(btnShow, 0, 0, 1, 1); //add button to layout
    16. connect(btnShow,SIGNAL(clicked()),this,SLOT(showLabels())); //connect the signal
    17.  
    18. container = new QWidget(centralWidget); //create the container
    19. layout->addWidget(container, 1, 0, 1, 1);
    20.  
    21. containerLayout = new QVBoxLayout(container); //create a layout for the container
    22.  
    23. label1 = new QLabel("test 1",container); //create the labels
    24. containerLayout->addWidget(label1); //add the labels to the layout
    25. label2 = new QLabel("test 2",container);
    26. containerLayout->addWidget(label2);
    27. label3 = new QLabel("test 3",container);
    28. containerLayout->addWidget(label3);
    29.  
    30. container->hide(); //hide the container
    31.  
    32. setCentralWidget(centralWidget); //set the central widget for the mainwindow
    33. }
    34.  
    35. MainWindow::~MainWindow()
    36. {
    37.  
    38. }
    39.  
    40. void MainWindow::showLabels()
    41. {
    42. if (container->isVisible())
    43. container->hide();
    44. else
    45. container->show();
    46. }
    To copy to clipboard, switch view to plain text mode 


    PS:
    it is always nice to have the desgner and see what code it generates

  5. The following 2 users say thank you to StrikeByte for this useful post:

    mahesh113 (5th June 2012), prabhudev (1st June 2012)

  6. #5
    Join Date
    May 2012
    Posts
    33
    Thanks
    9
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: hiding textbox and other widgets when loading a form

    @StrikeByte
    Thanks A Lot

Similar Threads

  1. Replies: 7
    Last Post: 23rd May 2012, 12:00
  2. Replies: 1
    Last Post: 12th May 2011, 22:41
  3. qgridlayout and hiding widgets
    By Michael_Levin in forum Newbie
    Replies: 3
    Last Post: 21st May 2010, 17:47
  4. Hiding a form and opening another form
    By anafor2004 in forum Newbie
    Replies: 1
    Last Post: 20th February 2008, 15:04
  5. Performance in hiding/showing widgets
    By Paalrammer in forum Newbie
    Replies: 12
    Last Post: 14th February 2007, 18:57

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.