0

What it should do: When the QComboBox is on the first item (index 0) it should hide the QStackedWidget. Which causes the QComboBox to extend as much as possible. As soon as you change the item in the QComboBox to anything, the QComboxBox should shrink and the QStackedWidget should display the correct page.

enter image description here

What I have and what it does instead: I test what the current index item is of the QComboBox and depending on that I change the size policies and visibility of widgets in order to obtain what I want. But it doesn't work. I tried to do workarounds but I cant seem to figure this out.

I also used qDebug() to see what currentIndexItem returns, and it seems to be stuck at 0, no matter to what index I change the QComboBox to. Do I have to update the currentItemIndex?

enter image description here

Note: I have a signal connected in the designer from QComboBox to QStackedWidget: currentIndexChanged(int) -> setCurrentIndex(int)

Here is my code.

My code:

interfacewindow.h

#ifndef INTERFACEWINDOW_H
#define INTERFACEWINDOW_H

#include <QMainWindow>
#include <QPainter>
#include <QComboBox>

namespace Ui
{
    class InterfaceWindow;
}

class InterfaceWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit InterfaceWindow(QWidget *parent = 0);
    ~InterfaceWindow();

private:
    Ui::InterfaceWindow *ui;

private slots:
    void on_actionClose_triggered();
};

#endif // INTERFACEWINDOW_H

interfacewindow.cpp

#include "interfacewindow.h"
#include "ui_interfacewindow.h"
#include <QDebug>

InterfaceWindow::InterfaceWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::InterfaceWindow)
{
    ui->setupUi(this);

    if(ui->comboBox->currentIndex() == 0)
    {
        ui->comboBox->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Fixed);
        ui->stackedWidget->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Fixed);
        ui->stackedWidget->setVisible(false);
    }
    else
    {
        ui->comboBox->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Fixed);
        ui->stackedWidget->setVisible(true);
    }
}

InterfaceWindow::~InterfaceWindow()
{
    delete ui;
}

void InterfaceWindow::on_actionClose_triggered()
{
    this->close();
}

main.cpp

#include <QApplication>

#include "interfacewindow.h"

int main(int argc, char **argv)
{
    QApplication a(argc, argv);
    InterfaceWindow w;
    w.show();
    return a.exec();
}
p.i.g.
  • 2,815
  • 2
  • 24
  • 41
  • 1
    It looks like the only place you take an action based on the state of ui->comboBox->currentIndex() is inside your InterfaceWindow class's constructor method. If you want your setSizePolicy()/setVisible() code to execute in response to a change of the QComboBox's state, you'll need to move it into a slot instead, and connect the QComboBox's currentIndexChanged() method to that slot-method. (You may want to manually call that slot-method from within the constructor as well, just to set up the initial state of the window to the desired state corresponding the QComboBox's initial state) – Jeremy Friesner Mar 12 '15 at 04:22
  • Alright i'll try that – user3752929 Mar 12 '15 at 13:37
  • That worked perfectly. I added a slot to the interface window and it gets triggered each time the combobox is activated? Like so: `connect(ui->comboBox, SIGNAL(activated(int)), this, SLOT(trigger(int)))` All the if statement from the constructor got moved to this slot and now it works perfectly. – user3752929 Mar 12 '15 at 19:51

0 Answers0