Results 1 to 6 of 6

Thread: QStackedWidgets and other Layouts does not display properly

  1. #1
    Join Date
    Mar 2013
    Location
    Hyderabad,Bangalore,India
    Posts
    70
    Thanks
    8
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default QStackedWidgets and other Layouts does not display properly

    following is my problem statement:

    create a horizantal layout in display
    add stackedwidget to this Hlayout
    the stackedwidget contains imagesWidget and loadWidget
    the imagesWidget is further divided into gridlayout which contains some icons
    so on the stackedwidget i have a plain widget and a widget withe gridlayout

    now , when i click on any icon of the gridlayout it should open loadwidget and on to that
    my application should be visible(here i am just playing with colors instead of applications)
    for this purpose i again divide grid layout into Horizantal layout and adding the apps widget.

    i face two problems here:

    1) my gridlayout icons on imagesWidget are coming properly but when i select them i can see
    under those icons same icons are present( i dont understand how i got duplicate)

    2) when i close my application i am following the steps mentioned in exitapp code:

    but my background of the loadWidget is still retianed but with icons from imageWidget

    unfortunately i cannot paste the connect calls because the loading is not handled by connect
    but some external events from some other module.

    below is list of steps i am following.


    ------------------------------------------
    Qt Code:
    1. controlsLayout=new QHBoxLayout();
    2. display->setLayout(controlsLayout);
    3.  
    4. imagesWidget = new QWidget;
    5. loadWidget = new QWidget;
    6.  
    7. QGridLayout *imagesLayout=new QGridLayout();
    8.  
    9. controlsLayout=new QHBoxLayout();
    10. display->setLayout(controlsLayout);
    11.  
    12. imagesWidget = new QWidget;
    13. loadWidget = new QWidget;
    14.  
    15. imagesWidget->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Preferred);
    16. imagesWidget->setLayout(imagesLayout);// setting a grid layout for imagesWidget
    17. imgstack = new QStackedWidget; // new stacked widget for imagesWidget and loadwidget
    18. imgstack->addWidget(imagesWidget);
    19. imgstack->addWidget(loadWidget);
    20. qDebug("imgstack cnt = %d",imgstack->count());
    21.  
    22. controlsLayout->addWidget(imgstack); // stacked widget to horizantal layout of of display
    23.  
    24. appstack = new QStackedWidget; // new stacked widget for applications
    25. const char *colors[]={"background-color: white",
    26. "background-color: black",
    27. "background-color: blue",
    28. "background-color: green",
    29. "background-color: yellow",
    30. "background-color: red",
    31. "background-color: pink",
    32. "background-color: cyan",
    33. "background-color: white"
    34. }; // unique colors are given for each widget to identify
    35.  
    36.  
    37. for(int apno = 0;apno < 9;apno ++){
    38. appwidgs[apno] = new QWidget;
    39. appwidgs[apno]->setStyleSheet(colors[apno]);
    40. appstack->addWidget(appwidgs[apno]);
    41. }
    To copy to clipboard, switch view to plain text mode 

    --------------------------
    upon click on any of the image the following will be performed.

    Qt Code:
    1. imgstack->setCurrentIndex(1);
    2. //loadWidget->setStyleSheet("background-color: blue");
    3. qDebug("currindx=%d\n",imgstack->currentIndex());
    4.  
    5. loadapp();
    To copy to clipboard, switch view to plain text mode 
    --------------------------
    loadapp code:

    Qt Code:
    1. loadWidget->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Preferred);
    2. loadWidget->setLayout(hl);
    3. hl->addWidget(appstack,0,0);
    4.  
    5. qDebug("curr_apps =%d",appstack->currentIndex());
    6. appstack->setCurrentIndex(currpos);
    To copy to clipboard, switch view to plain text mode 
    ---------------------------------------

    exitapp code:

    Qt Code:
    1. appstack->removeWidget(appwidgs[currpos]);
    2. imgstack->removeWidget(loadWidget);
    3. imgstack->setCurrentWidget(imagesWidget);
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QStackedWidgets and other Layouts does not display properly

    If you have a navigation panel of 9 buttons, each of which should switch to a different 'application' then surely you want the single stacked widget to have 10 panels: the navigation panel and each of the 9 applications.

    Here's an example:
    Qt Code:
    1. #include <QtGui>
    2.  
    3. class MainWindow: public QMainWindow
    4. {
    5. Q_OBJECT
    6.  
    7.  
    8. public:
    9. explicit MainWindow(QWidget *p = 0): QMainWindow(p)
    10. {
    11. resize(512, 512);
    12. stack = new QStackedWidget(this);
    13. setCentralWidget(stack);
    14.  
    15. // Plumbing
    16. QSignalMapper *mapper = new QSignalMapper(stack);
    17. connect(mapper, SIGNAL(mapped(int)), stack, SLOT(setCurrentIndex(int)));
    18.  
    19. // Nav panel
    20. QWidget *panel = new QWidget(this);
    21. stack->addWidget(panel); // index 0
    22.  
    23. QGridLayout *grid = new QGridLayout(this);
    24. panel->setLayout(grid);
    25. for (int r = 0; r < 3; ++r) {
    26. for (int c = 0; c < 3; ++c) {
    27. // Create the navigation button
    28. const int index = r * 3 + c + 1;
    29. QPushButton *navButton = new QPushButton(QString("%1").arg(index), panel);
    30. grid->addWidget(navButton, r, c, Qt::AlignCenter );
    31.  
    32. // and the matching 'application'
    33. QPushButton *button = new QPushButton(QString("App %1, click to end").arg(index), stack);
    34. stack->addWidget(button);
    35. connect(button, SIGNAL(clicked()), SLOT(goHome())); // needs some way to go back
    36.  
    37. // make the nav button switch
    38. mapper->setMapping(navButton, index);
    39. connect(navButton, SIGNAL(clicked()), mapper, SLOT(map()));
    40. }
    41. }
    42. }
    43.  
    44. public slots:
    45. void goHome()
    46. {
    47. stack->setCurrentIndex(0);
    48. }
    49. };
    50.  
    51. int main(int argc, char **argv)
    52. {
    53. QApplication app(argc, argv);
    54. MainWindow w;
    55. w.show();
    56. return app.exec();
    57. }
    58. #include "main.moc"
    To copy to clipboard, switch view to plain text mode 
    Last edited by ChrisW67; 2nd April 2013 at 00:42.

  3. #3
    Join Date
    Mar 2013
    Location
    Hyderabad,Bangalore,India
    Posts
    70
    Thanks
    8
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QStackedWidgets and other Layouts does not display properly

    Thanks ChrisW,

    i have done almost the same way , but my problem is , on my 3X3 layout window i am getting duplicate buttons by overlapping on existing.

    and one more problem is when i use removeWidget :
    suppose say i have displayed an application, now i want to go back to my image window where i have list of icons for naviagtion.
    i am following the statements below.

    appstack->removeWidget(appwidgs[currpos]); // to remove the crrent application upon exit
    imgstack->removeWidget(loadWidget); // to remove widget used for application display
    imgstack->setCurrentWidget(imagesWidget);// set images widget as current display

    i am getting my images widget but , other two widgets are not geting removed.
    i understand remove widget means , just hiding the widget to make path for other widgets.

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QStackedWidgets and other Layouts does not display properly

    Read my example again: it's your entire program in a nutshell. My point is that you do not need to do anything other than to switch pages in the single QStackedWidget. No adding or removing widgets after the initial construction, no multiple stacked widgets, nothing special to do during destruction.

  5. #5
    Join Date
    Mar 2013
    Location
    Hyderabad,Bangalore,India
    Posts
    70
    Thanks
    8
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QStackedWidgets and other Layouts does not display properly

    I now follow the same way as you suggested, but still i am getting the same problem.
    i really need to solve this.
    please have a look.
    Qt Code:
    1. QHBoxLayout controlsLayout=new QHBoxLayout();
    2. display->setLayout(controlsLayout);
    3.  
    4. imgstack = new QStackedWidget;
    5. imagesWidget = new QWidget;
    6. imgstack->addWidget(imagesWidget);
    7.  
    8. QGridLayout *imagesLayout=new QGridLayout();
    9. imagesWidget->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Preferred);
    10.  
    11. no_of_icons =9;
    12.  
    13. RowCount=no_of_icons/COL +
    14. (temp =(no_of_icons%COL?1:0));
    15.  
    16. temp=0;
    17. for( i=0;i<RowCount;i++)
    18. {
    19. for(j=0;j<3;j++)
    20. {
    21. Buttons[temp]=new QPushButton();
    22.  
    23. QPixmap pixmap(":/images/img.jpeg");
    24. QIcon ButtonIcon(pixmap);
    25. Buttons[temp]->setIcon(ButtonIcon);
    26. Buttons[temp]->setIconSize(pixmap.rect().size());
    27.  
    28. Buttons[temp]->setMinimumWidth(80);
    29. Buttons[temp]->setMinimumHeight(80);
    30. Buttons[temp]->setMaximumWidth(80);
    31. Buttons[temp]->setMaximumHeight(80);
    32.  
    33. QLabel *label = new QLabel("Button",Buttons[temp],0);
    34. label->setAlignment(Qt::AlignVCenter|Qt::AlignVCenter);
    35. label->show();
    36. imagesLayout->addWidget(Buttons[temp],i,j);
    37. temp++;
    38. if(imagesLayout->count() ==no_of_icons)
    39. break;
    40. }
    41.  
    42. }
    43.  
    44. currpos=0;
    45.  
    46. if(no_of_icons>0)
    47. {
    48. QPixmap pixmap(":/images/image.jpeg");
    49. QIcon ButtonIcon(pixmap);
    50.  
    51. Buttons[currpos]->setIcon(ButtonIcon);
    52. Buttons[currpos]->setIconSize(pixmap.rect().size());
    53. Buttons[currpos]->setMinimumWidth(68+10);
    54. Buttons[currpos]->setMinimumHeight(60+10);
    55. Buttons[currpos]->setMaximumWidth(68+10);
    56. Buttons[currpos]->setMaximumHeight(68+10);
    57.  
    58. }
    59. imagesWidget->setLayout(imagesLayout);
    60. qDebug("imgstack cnt = %d",imgstack->count());
    61. const char *colors[]={"background-color: cyan",
    62. "background-color: grey",
    63. "background-color: blue",
    64. "background-color: green",
    65. "background-color: yellow",
    66. "background-color: red",
    67. "background-color: pink",
    68. "background-color: white",
    69. "background-color: orange"
    70. };
    71.  
    72.  
    73. for(int apno = 0;apno < 9;apno ++){
    74. appwidgs[apno] = new QWidget;
    75. appwidgs[apno]->setStyleSheet(colors[apno]);
    76. imgstack->addWidget(appwidgs[apno]);
    77. }
    78.  
    79. controlsLayout->addWidget(imgstack);
    80. qDebug("imgstack_count = %d",imgstack->count());
    To copy to clipboard, switch view to plain text mode 

    -----------------------
    currpos = 0 ; // will be incremented based on no of left and right arrows
    in loadapp()
    Qt Code:
    1. imgstack->setCurrentIndex(currpos+1); //loads the widget lets say i select index 3(widget:blue)
    2. printf("load:curridx =%d",imgstack->currentIndex());
    To copy to clipboard, switch view to plain text mode 
    -------------------------
    in exitapp() // will be invoked when i press exit button
    Qt Code:
    1. imgstack->setCurrentIndex(0); // widget at index 0 is loaded,but still the blue color can be seen.
    2. printf("exit:curridx =%d",imgstack->currentIndex());
    To copy to clipboard, switch view to plain text mode 

    -------------
    out put: (when i load the 0th app that is placed at index 1.)

    imgstack cnt = 1
    imgstack_count = 10

    load :currindx=1
    exit:curridx =0

  6. #6
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QStackedWidgets and other Layouts does not display properly

    Your code fragment, while full of cruft, is not fundamentally broken. However, I can only guess what the rest of you program is doing or even where/when you are calling this code from. I could guess that you call this code several times and lay one layout over another... but it's only a WAG. Produce a simple, complete program that demonstrates the problem and post it.

Similar Threads

  1. Replies: 9
    Last Post: 28th March 2011, 22:51
  2. QWebView does not display Specific font properly ?
    By mismael85 in forum Qt Programming
    Replies: 4
    Last Post: 8th February 2011, 16:05
  3. Qt with DirectFB won't display properly.
    By Francach in forum Qt for Embedded and Mobile
    Replies: 1
    Last Post: 16th September 2010, 20:46
  4. Replies: 0
    Last Post: 4th November 2009, 18:15
  5. Qt Desiger don't display the Form properly
    By bzyx in forum Qt Tools
    Replies: 4
    Last Post: 8th December 2008, 15:05

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.