QStackedWidgets problems



  • Iam trying to :
    1) create a horizantal layout in display (basically a QWidget)
    2) add stackedwidget to this Hlayout
    3) the stackedwidget contains imagesWidget and loadWidget
    4) 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.


      @controlsLayout=new QHBoxLayout();
      display->setLayout(controlsLayout);
    
      imagesWidget = new QWidget;
      loadWidget = new QWidget;
    
      QGridLayout *imagesLayout=new QGridLayout();
    
      controlsLayout=new QHBoxLayout();
      display->setLayout(controlsLayout);
    
      imagesWidget = new QWidget;
      loadWidget = new QWidget;
    
      imagesWidget->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Preferred);
      imagesWidget->setLayout(imagesLayout);// setting a grid layout for imagesWidget
      imgstack = new QStackedWidget; // new stacked widget  for imagesWidget and loadwidget
      imgstack->addWidget(imagesWidget);
      imgstack->addWidget(loadWidget);
      qDebug("imgstack cnt = %d",imgstack->count());
    
      controlsLayout->addWidget(imgstack); // stacked widget to horizantal layout of of display
    
      appstack = new QStackedWidget; // new stacked widget for applications
      const char *colors[]={"background-color: white", 
                          "background-color: black",
                          "background-color: blue",
                          "background-color: green",
                          "background-color: yellow",
                          "background-color: red",
                          "background-color: pink",
                          "background-color: cyan",
                          "background-color: white"
                           }; // unique colors are given for each widget to identify
    
    
      for(int apno = 0;apno < 9;apno ++){
          appwidgs[apno] = new QWidget;
           appwidgs[apno]->setStyleSheet(colors[apno]);
         appstack->addWidget(appwidgs[apno]);
      }
    

    @

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

    @imgstack->setCurrentIndex(1);
    //loadWidget->setStyleSheet("background-color: blue");
    qDebug("currindx=%d\n",imgstack->currentIndex());

               loadapp();@
    

    loadapp code:

    @ QHBoxLayout *hl = new QHBoxLayout;
    loadWidget->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Preferred);
    loadWidget->setLayout(hl);
    hl->addWidget(appstack,0,0);

    qDebug("curr_apps =%d",appstack->currentIndex());
    appstack->setCurrentIndex(currpos);
    

    @

    exitapp code:

    @ appstack->removeWidget(appwidgs[currpos]);
    imgstack->removeWidget(loadWidget);
    imgstack->setCurrentWidget(imagesWidget);
    @



  • Why did you call the following two lines twice:

    @controlsLayout=new QHBoxLayout();
    display->setLayout(controlsLayout);@

    Please clean up your code try to keep releated things together.
    How about Top-Down design.

    Start with the most top layout.
    Create all widgets releated to this layout.
    Add those widget to this layout.

    You can do it bottom up as well.

    @controlsLayout=new QHBoxLayout(); // top layout

    imgstack = new QStackedWidget; // new stacked widget for imagesWidget and loadwidget
    loadWidget = new QWidget;
    imagesWidget = new QWidget;
    QGridLayout *imagesLayout=new QGridLayout();

    imagesWidget->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Preferred);
    imagesWidget->setLayout(imagesLayout);// setting a grid layout for imagesWidget

    imgstack->addWidget(imagesWidget);
    imgstack->addWidget(loadWidget);
    qDebug("imgstack cnt = %d",imgstack->count());

    appstack = new QStackedWidget; // new stacked widget for applications
    //The rest of your code...

    controlsLayout->addWidget(imgstack); // stacked widget to horizantal layout of of
    controlsLayout->addWidget(appstack);

    display->setLayout(controlsLayout);@

    1. Question:

    Which is the second widget in the QHBoxLayout?
    appStack?



  • [quote author="messi" date="1364890538"]
    Why did you call the following two lines twice:

    @controlsLayout=new QHBoxLayout();
    display->setLayout(controlsLayout);@

    [/quote]

    i apologies for my oversight , i mistakenly copied the second time.

    Please clean up your code try to keep releated things together.
    How about Top-Down design.

    Start with the most top layout.
    Create all widgets releated to this layout.
    Add those widget to this layout.

    You can do it bottom up as well.

    [quote author="messi" date="1364890538"]

    1. Question:

    Which is the second widget in the QHBoxLayout?
    appStack? [/quote]

           *loadWidget*
    

    I have cleaned up and modified the code a bit but still have two problems:

    prob1:
    whenever i navigate through the buttons i can see the duplicate buttons under buttons(two copies)

    prob2:
    previous widgets color is retaining when i exit the app

    [CODE]

      QHBoxLayout controlsLayout=new QHBoxLayout();
      display->setLayout(controlsLayout);
    
      imgstack = new QStackedWidget;
      imagesWidget = new QWidget;
      imgstack->addWidget(imagesWidget);
    
      QGridLayout *imagesLayout=new QGridLayout();
      imagesWidget->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Preferred);
    
      no_of_icons =9;
    
      RowCount=no_of_icons/COL +
                (temp =(no_of_icons%COL?1:0));
    
      temp=0;
      for( i=0;i<RowCount;i++)
      {
          for(j=0;j<3;j++)
          {
           Buttons[temp]=new QPushButton();
    
           QPixmap pixmap(":/images/img.jpeg");
           QIcon ButtonIcon(pixmap);
           Buttons[temp]->setIcon(ButtonIcon);
           Buttons[temp]->setIconSize(pixmap.rect().size());
    
           Buttons[temp]->setMinimumWidth(80);
           Buttons[temp]->setMinimumHeight(80);
           Buttons[temp]->setMaximumWidth(80);
           Buttons[temp]->setMaximumHeight(80);
    
            QLabel *label = new QLabel("Button",Buttons[temp],0);
            label->setAlignment(Qt::AlignVCenter|Qt::AlignVCenter);
            label->show();
            imagesLayout->addWidget(Buttons[temp],i,j);
            temp++;
            if(imagesLayout->count() ==no_of_icons)
            break;
          }
    
        }
    
    currpos=0;
    
      if(no_of_icons>0)
      {
          QPixmap pixmap(":/images/image.jpeg");
          QIcon ButtonIcon(pixmap);
    
         Buttons[currpos]->setIcon(ButtonIcon);
         Buttons[currpos]->setIconSize(pixmap.rect().size());
         Buttons[currpos]->setMinimumWidth(68+10);
         Buttons[currpos]->setMinimumHeight(60+10);
         Buttons[currpos]->setMaximumWidth(68+10);
         Buttons[currpos]->setMaximumHeight(68+10);
    
      }
    imagesWidget->setLayout(imagesLayout);
    qDebug("imgstack cnt = %d",imgstack->count());
    const char *colors[]={"background-color: cyan",
                          "background-color: grey",
                          "background-color: blue",
                          "background-color: green",
                          "background-color: yellow",
                          "background-color: red",
                          "background-color: pink",
                          "background-color: white",
                          "background-color: orange"
                           };
    
    
      for(int apno = 0;apno < 9;apno ++){
          appwidgs[apno] = new QWidget;
          appwidgs[apno]->setStyleSheet(colors[apno]);
          imgstack->addWidget(appwidgs[apno]);
      }
    
    controlsLayout->addWidget(imgstack);
    qDebug("imgstack_count = %d",imgstack->count());
    

    currpos = 0 ; // will be incremented based on no of left and right arrows
    in loadapp()
    imgstack->setCurrentIndex(currpos+1); //loads the widget lets say i select index 3(widget:blue)
    printf("load:curridx =%d",imgstack->currentIndex());


    in exitapp() // will be invoked when i press exit button
    imgstack->setCurrentIndex(0); // widget at index is loaded,but still the blue color can be seen.
    printf("exit:curridx =%d",imgstack->currentIndex());

    [/CODE]


    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



  • @ no_of_icons =9;

      RowCount=no_of_icons/COL +
                (temp =(no_of_icons%COL?1:0));
    
      temp=0;
      for( i=0;i<RowCount;i++)
      {
          for(j=0;j<3;j++)@
    

    Very strange code.
    no_of_icons is const
    COL is const
    In that case you can set RowCount also to const.
    Can you explain that?


  • Lifetime Qt Champion

    Hi,

    If I read you code correctly you are setting the icon twice, resizing it twice and also resizing the buttons twice.

    Could it be that what you see is some "leftover" of the first icon ?



  • @ Buttons[temp]->setMinimumWidth(80);
    Buttons[temp]->setMinimumHeight(80);
    Buttons[temp]->setMaximumWidth(80);
    Buttons[temp]->setMaximumHeight(80);@

    How about using QWidget::setFixedSize() instead



  • [quote author="Juergen_M" date="1364897742"]@ no_of_icons =9;

      RowCount=no_of_icons/COL +
                (temp =(no_of_icons%COL?1:0));
    
      temp=0;
      for( i=0;i<RowCount;i++)
      {
          for(j=0;j<3;j++)@
    

    Very strange code.
    no_of_icons is const
    COL is const
    In that case you can set RowCount also to const.
    Can you explain that?[/quote]

    The code is actually messed up , because it was implemented by 2-3 people, I have actually modified the code by deleting repeated portions. I will post it soon..

    [quote author="SGaist" date="1364897797"]
    Hi,
    If I read you code correctly you are setting the icon twice, resizing it twice and also resizing the buttons twice.

    Could it be that what you see is some "leftover" of the first icon ?
    [/quote]

    I suspect so , but i have observed the same problem(duplicate images) in one scenario from a friend.

    Suppose say i have some widget which is one of the widgets of some other laidout widgets.
    now i am trying to place a new widget on this laid out widget(by creating some layout or widget =new QWidget(basewidget)) and create some buttons place them in a Grid or Hlayout then i can see some duplicate images.

    in my case display was the base widget, and i was trying to place a new widget which is a stackedwidget imgstack by creating display->setLayout(controlsLayout); ( this might be the problem)
    add new widgets to stackedwidget
    one of these new widgets is agian having a grid layout( imagesWidget ).(this might also be a problem)

    if any one could help me it would a great .... :)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.