Page 1 of 2 12 LastLast
Results 1 to 20 of 22

Thread: How to stop multiple appearance of a widget

  1. #1
    Join Date
    Mar 2006
    Location
    Hyderabad
    Posts
    69
    Thanks
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default How to stop multiple appearance of a widget

    hi all,
    I have an application in which when I press a button I should get a find dialog box. Thing is if I press the button again, I shouldnot get one more dialog box. The previous one should appear.
    My present code is like this:
    Qt Code:
    1. CPT::CPT(QWidget *parent):QWidget(parent)
    2. {
    3. //some other lines of code
    4. QPushButton *but=new QPushButton(this);
    5. QObject::connect(but,SIGNAL(clicked()),this,SLOT(findbox()));
    6. //remaining program
    7. }
    8. void CPT::findbox()
    9. {
    10. QWidget *findstr=new QWidget;
    11. //other widgets added to it
    12. findstr->show();
    13. }
    To copy to clipboard, switch view to plain text mode 
    So kindly help me in this problem.

  2. #2
    Join Date
    Jan 2006
    Location
    Kerala
    Posts
    371
    Thanks
    76
    Thanked 37 Times in 32 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to stop multiple appearance of a widget

    Qt Code:
    1. Window::showFindDialog(){
    2. static FindDialog* pFindDlg = 0;
    3. if( pFindDlg == 0 ) {
    4. pFindDlg = new FindDialog( this, "") ;
    5. }
    6. pFindDlg->show();
    7. pFindDlg->raise();
    8. pFindDlg->setActiveWindow();
    9.  
    10. }
    To copy to clipboard, switch view to plain text mode 
    We can't solve problems by using the same kind of thinking we used when we created them

  3. #3
    Join Date
    Mar 2006
    Location
    Hyderabad
    Posts
    69
    Thanks
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default Re: How to stop multiple appearance of a widget

    hi sunil,
    can you please show me the changes that I have to make in my program. And u have written FindDialog in your code. Is there any such default dialog box provided by Qt.

    regards,
    Sarma.

  4. #4
    Join Date
    Jan 2006
    Location
    Kerala
    Posts
    371
    Thanks
    76
    Thanked 37 Times in 32 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to stop multiple appearance of a widget

    I just called the Dialog you wanted to show as FindDialog That is all !
    We can't solve problems by using the same kind of thinking we used when we created them

  5. #5
    Join Date
    Mar 2006
    Location
    Hyderabad
    Posts
    69
    Thanks
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default Re: How to stop multiple appearance of a widget

    sorry sunil,
    That is giving good number of errors. It says that the second line (of your code) is wrong and because of this, it says that pFindDlg is not defined. can you suggest me other method

  6. #6
    Join Date
    Jan 2006
    Location
    Kerala
    Posts
    371
    Thanks
    76
    Thanked 37 Times in 32 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to stop multiple appearance of a widget

    Try the Attachment
    Attached Files Attached Files
    We can't solve problems by using the same kind of thinking we used when we created them

  7. #7
    Join Date
    Mar 2006
    Location
    Hyderabad
    Posts
    69
    Thanks
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default Re: How to stop multiple appearance of a widget

    hi sunil,
    well, thanks for your help. But no where I have found the code that you have written previously. In which file should I see that . And can you please tell me how to generate the .ui files and use them in our ordinary program.

    Thanks & regards,
    Sarma.

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,363
    Thanks
    3
    Thanked 5,012 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to stop multiple appearance of a widget


  9. #9
    Join Date
    Mar 2006
    Location
    Hyderabad
    Posts
    69
    Thanks
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default Re: How to stop multiple appearance of a widget

    sir,
    Iam getting errors as I said previously . Kindly show me the changes that I have to make in my code(I have send it in my first post).

    Thanks & regards,
    Sarma.

  10. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,363
    Thanks
    3
    Thanked 5,012 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to stop multiple appearance of a widget

    Look at the FAQ entry I mentioned. The code is there -- literally. Especially note the "static" keyword.

  11. #11
    Join Date
    Mar 2006
    Location
    Hyderabad
    Posts
    69
    Thanks
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default Re: How to stop multiple appearance of a widget

    sir,
    I have modified my code like this:
    Qt Code:
    1. CPT::CPT(QWidget *parent):QWidget(parent)
    2. {
    3. //some other lines of code
    4. QPushButton *but=new QPushButton(this);
    5. QObject::connect(but,SIGNAL(clicked()),this,SLOT(findbox()));
    6. //remaining program
    7. }
    8. void CPT::findbox()
    9. {
    10. QWidget *fwin=new QWidget;
    11. //other widgets added to it
    12. //fwin->show();
    13. static fwin *form2 = new fwin(this);
    14. form2->show();
    15. form2->activateWindow(); // or form2->setActiveWindow() in Qt3
    16. form2->raise();
    17. }
    To copy to clipboard, switch view to plain text mode 

    And I got the following errors:

    "cptdebug.cpp", line 1501: Error: fwin, of type int, was previously declared QWidget*.
    "cptdebug.cpp", line 1501: Error: Multiple declaration for fwin.
    "cptdebug.cpp", line 1501: Error: "," expected instead of "*".
    "cptdebug.cpp", line 1502: Error: form2 is not defined.
    "cptdebug.cpp", line 1503: Error: form2 is not defined.
    "cptdebug.cpp", line 1504: Error: form2 is not defined.
    Kindly tell me why? And give me the possible solution.

    Thanks and regards,
    Sarma.
    Last edited by Sarma; 9th March 2006 at 09:22.

  12. #12
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,363
    Thanks
    3
    Thanked 5,012 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to stop multiple appearance of a widget

    The compiler already told you why...

    Qt Code:
    1. void CPT::findbox(){
    2. static fwin *form2 = new fwin(this);
    3. form2->show();
    4. form2->activateWindow();
    5. form2->raise();
    6. }
    To copy to clipboard, switch view to plain text mode 

  13. #13
    Join Date
    Mar 2006
    Location
    Hyderabad
    Posts
    69
    Thanks
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default Re: How to stop multiple appearance of a widget

    Sir,
    sorry for troubling you much. I couldnot get the proper solution.Please show the changes that I have to make in my code.

    regards,
    Sarma.

  14. #14
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,363
    Thanks
    3
    Thanked 5,012 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to stop multiple appearance of a widget

    Well, I gave you the code in the previous post. What else do you want?

  15. #15
    Join Date
    Mar 2006
    Location
    Hyderabad
    Posts
    69
    Thanks
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default Re: How to stop multiple appearance of a widget

    hello Sir,
    I have posted this thread long back but I couldnot get proper solution.
    Quote Originally Posted by wysota
    In that FAQ, it was written that "Assuming you have two form classes: Form1 and Form2 and that Form1 has a pushbutton "button" child and Form2 is derived from QDialog" .

    But in my application, the find box which I want to show is not derived from QDialog. It is a widget to which many child widgets are added.
    Here is the code that I have written:

    Qt Code:
    1. CPT::CPT(QWidget *parent):QWidget (parent)
    2. {
    3. QPushButton *findbut=new QPushButton(this);
    4. QObject::connect(findbut,SIGNAL(clicked()),this,SLOT(findstr()));
    5. }
    6.  
    7. void CPT::findstr()
    8. {
    9. QWidget *fwin=new QWidget;
    10. fwin->setCaption("CP Trace Debug Tool : FIND");
    11. fwin->setFixedSize(320,125);
    12. fwin->setFont(QFont("SansSerif",9));
    13. fwin->setBackgroundColor(c1);
    14.  
    15. QVBoxLayout *fvbox=new QVBoxLayout(fwin);
    16. fvbox->setMargin(2);
    17. fvbox->setSpacing(6);
    18.  
    19. QHBoxLayout *fhbox1=new QHBoxLayout(fvbox);
    20. fhbox1->setMargin(2);
    21. fhbox1->setSpacing(8);
    22.  
    23. QLabel *fl1=new QLabel("Find:",fwin);
    24. fl1->setBackgroundColor(c1);
    25.  
    26. fedit1=new QLineEdit(fwin);
    27.  
    28. fch=new QCheckBox("Regular Expression",fwin);
    29. fch->setBackgroundColor(c1);
    30.  
    31. fhbox1->addWidget(fl1);
    32. fhbox1->addWidget(fedit1);
    33. fhbox1->addWidget(fch);
    34.  
    35. QHBoxLayout *fhbox2=new QHBoxLayout(fvbox);
    36. fhbox2->setMargin(2);
    37. fhbox2->setSpacing(6);
    38.  
    39. QPushButton *fb1=new QPushButton("Forward",fwin);
    40. fb1->setPaletteBackgroundColor(c1);
    41.  
    42. QPushButton *fb2=new QPushButton("Backward",fwin);
    43. fb2->setPaletteBackgroundColor(c1);
    44.  
    45. QPushButton *fb3=new QPushButton("Close",fwin);
    46. fb3->setPaletteBackgroundColor(c1);
    47.  
    48. QObject::connect(fb1,SIGNAL(clicked()),this,SLOT(result_for()));
    49. QObject::connect(fb2,SIGNAL(clicked()),this,SLOT(result_bck()));
    50. QObject::connect(fb3,SIGNAL(clicked()),fwin,SLOT(close()));
    51.  
    52. fhbox2->addWidget(fb1);
    53. fhbox2->addWidget(fb2);
    54. fhbox2->addWidget(fb3);
    55.  
    56. fwin->show();
    57. }
    To copy to clipboard, switch view to plain text mode 

    Now when I press the button again and again, I get multiple windows of this findbox which I shouldn't. Kindly give me the possible solution.

    Thanks & Regards,
    Sarma.

  16. #16
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,518 Times in 1,388 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: How to stop multiple appearance of a widget

    You are always creating a new find dialog each and every time the button is pressed.
    Move the find widget as a member variable, instantiate it only once, and connect the find buttons clicked() signal to the find widget's show() slot.

    Qt Code:
    1. CPT::CPT(QWidget *parent):QWidget (parent)
    2. {
    3. createFind();
    4. QPushButton *findbut=new QPushButton(this);
    5. QObject::connect(findbut,SIGNAL(clicked()),fwin,SLOT(show()));
    6. }
    7.  
    8. void CPT::createFind()
    9. {
    10. // add "QWidget* fwin" as a member variable
    11. fwin=new QWidget;
    12. fwin->setCaption("CP Trace Debug Tool : FIND");
    13. fwin->setFixedSize(320,125);
    14. fwin->setFont(QFont("SansSerif",9));
    15. fwin->setBackgroundColor(c1);
    16.  
    17. QVBoxLayout *fvbox=new QVBoxLayout(fwin);
    18. fvbox->setMargin(2);
    19. fvbox->setSpacing(6);
    20.  
    21. QHBoxLayout *fhbox1=new QHBoxLayout(fvbox);
    22. fhbox1->setMargin(2);
    23. fhbox1->setSpacing(8);
    24.  
    25. QLabel *fl1=new QLabel("Find:",fwin);
    26. fl1->setBackgroundColor(c1);
    27.  
    28. fedit1=new QLineEdit(fwin);
    29.  
    30. fch=new QCheckBox("Regular Expression",fwin);
    31. fch->setBackgroundColor(c1);
    32.  
    33. fhbox1->addWidget(fl1);
    34. fhbox1->addWidget(fedit1);
    35. fhbox1->addWidget(fch);
    36.  
    37. QHBoxLayout *fhbox2=new QHBoxLayout(fvbox);
    38. fhbox2->setMargin(2);
    39. fhbox2->setSpacing(6);
    40.  
    41. QPushButton *fb1=new QPushButton("Forward",fwin);
    42. fb1->setPaletteBackgroundColor(c1);
    43.  
    44. QPushButton *fb2=new QPushButton("Backward",fwin);
    45. fb2->setPaletteBackgroundColor(c1);
    46.  
    47. QPushButton *fb3=new QPushButton("Close",fwin);
    48. fb3->setPaletteBackgroundColor(c1);
    49.  
    50. QObject::connect(fb1,SIGNAL(clicked()),this,SLOT(result_for()));
    51. QObject::connect(fb2,SIGNAL(clicked()),this,SLOT(result_bck()));
    52. QObject::connect(fb3,SIGNAL(clicked()),fwin,SLOT(close()));
    53.  
    54. fhbox2->addWidget(fb1);
    55. fhbox2->addWidget(fb2);
    56. fhbox2->addWidget(fb3);
    57.  
    58. // removed this
    59. // fwin->show();
    60. }
    To copy to clipboard, switch view to plain text mode 

  17. #17
    Join Date
    Mar 2006
    Location
    Hyderabad
    Posts
    69
    Thanks
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default Re: How to stop multiple appearance of a widget

    hi jpn,
    well thanks for replying.But that didnot solve my problem. Actually, I didnot put the entire code before you( sorry for that ). Thing is that I need to set certain values before createFind() is called. So, I have the code like( with the changes suggested by you ):
    Qt Code:
    1. CPT::CPT(QWidget *parent):QWidget(parent)
    2. {
    3. QPushButton *findbox=new QPushButton();
    4. QObject::connect(findbox,SIGNAL(clicked()),this,SLOT(defaults()));
    5. }
    6.  
    7. void CPT::defaults()
    8. {
    9. //setting values for some variables (of integer type)
    10. createFind();
    11. fwin->show();
    12. }
    13.  
    14. void CPT::createFind()
    15. {
    16. //QWidget *fwin is made as a member variable
    17. fwin=new QWidget;
    18. fwin->setCaption("CP Trace Debug Tool : FIND");
    19. fwin->setFixedSize(320,125);
    20. fwin->setFont(QFont("SansSerif",9));
    21. fwin->setBackgroundColor(c1);
    22.  
    23. QVBoxLayout *fvbox=new QVBoxLayout(fwin);
    24. fvbox->setMargin(2);
    25. fvbox->setSpacing(6);
    26.  
    27. QHBoxLayout *fhbox1=new QHBoxLayout(fvbox);
    28. fhbox1->setMargin(2);
    29. fhbox1->setSpacing(8);
    30.  
    31. QLabel *fl1=new QLabel("Find:",fwin);
    32. fl1->setBackgroundColor(c1);
    33.  
    34. fedit1=new QLineEdit(fwin);
    35.  
    36. fch=new QCheckBox("Regular Expression",fwin);
    37. fch->setBackgroundColor(c1);
    38.  
    39. fhbox1->addWidget(fl1);
    40. fhbox1->addWidget(fedit1);
    41. fhbox1->addWidget(fch);
    42.  
    43. QHBoxLayout *fhbox2=new QHBoxLayout(fvbox);
    44. fhbox2->setMargin(2);
    45. fhbox2->setSpacing(6);
    46.  
    47. QPushButton *fb1=new QPushButton("Forward",fwin);
    48. fb1->setPaletteBackgroundColor(c1);
    49.  
    50. QPushButton *fb2=new QPushButton("Backward",fwin);
    51. fb2->setPaletteBackgroundColor(c1);
    52.  
    53. QPushButton *fb3=new QPushButton("Close",fwin);
    54. fb3->setPaletteBackgroundColor(c1);
    55.  
    56. QObject::connect(fb1,SIGNAL(clicked()),this,SLOT(result_for()));
    57. QObject::connect(fb2,SIGNAL(clicked()),this,SLOT(result_bck()));
    58. QObject::connect(fb3,SIGNAL(clicked()),fwin,SLOT(close()));
    59.  
    60. fhbox2->addWidget(fb1);
    61. fhbox2->addWidget(fb2);
    62. fhbox2->addWidget(fb3);
    63.  
    64. // fwin->show();
    65. }
    To copy to clipboard, switch view to plain text mode 

    Now kindly show me changes that I can make in this program so that at any time only one box should be made visible.

    Thanks & Regards,
    Sarma.

  18. #18
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,518 Times in 1,388 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: How to stop multiple appearance of a widget

    ....and you are still creating the dialog again and again.
    You do not want to create a new instance of the dialog every time the button is pressed.

    Qt Code:
    1. CPT::CPT(QWidget *parent):QWidget(parent)
    2. {
    3. createFind(); // create the dialog ONLY ONCE
    4. QPushButton *findbox=new QPushButton();
    5. QObject::connect(findbox,SIGNAL(clicked()),this,SLOT(find()));
    6. }
    7.  
    8. void CPT::find()
    9. {
    10. // set your defaults here
    11. fwin->show();
    12. fwin->activateWindow();
    13. fwin->raise();
    14. }
    15.  
    16. void CPT::createFind()
    17. {
    18. fwin = new QWidget;
    19. // ...
    20. }
    To copy to clipboard, switch view to plain text mode 

  19. #19
    Join Date
    Mar 2006
    Location
    Hyderabad
    Posts
    69
    Thanks
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default Re: How to stop multiple appearance of a widget

    sir,
    Thank you very much. It is working well. Now I got the point. I was creating an instance of the window everytime I click the button. But here I got a doubt. We are creating the instance of the findbox in the application even before clicking it. Now for any reason if we donot click the button , doesn't that cause memory wastage? Kindly rectify my doubt.

    Thanks once again for this useful post,
    Sarma.

  20. #20
    Join Date
    Jan 2006
    Location
    travelling
    Posts
    1,116
    Thanks
    8
    Thanked 127 Times in 121 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to stop multiple appearance of a widget

    Quote Originally Posted by Sarma
    sir,
    Thank you very much. It is working well. Now I got the point. I was creating an instance of the window everytime I click the button. But here I got a doubt. We are creating the instance of the findbox in the application even before clicking it. Now for any reason if we donot click the button , doesn't that cause memory wastage? Kindly rectify my doubt.

    Thanks once again for this useful post,
    Sarma.
    No memory leaks caused provided you use destructor wisely to free all used memory...
    Current Qt projects : QCodeEdit, RotiDeCode

Similar Threads

  1. QDockWidget inside another widget in the center?
    By Antebios in forum Qt Programming
    Replies: 1
    Last Post: 16th February 2010, 07:06
  2. Tricky problem with ARGB widget / UpdateLayeredWindow
    By nooky59 in forum Qt Programming
    Replies: 3
    Last Post: 21st February 2008, 10:35
  3. Display multiple Images in a widget
    By jeetu_happy in forum Qt Programming
    Replies: 2
    Last Post: 7th March 2007, 11:24

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.