4

I'm working on a Configuration QDialog. It has several categories (General, Appearance, etc.) that get loaded when the user clicks on them. Each category has its own page. These pages are separate classes in their own right (each has its own ui, cpp, and h). When the Preferences window loads, the pages get instantiated and are loaded into a QStackedWidget. The Stackedwidget is then placed into a QScrollArea so it can scroll if necessary.

However, there is a problem. Ever since I added the QStackedWidget, the QScrollArea always has a vertical scrollbar even when the current page is short enough not to need one: (picture shows the shortest page)

Config scrollbar problem

The ScrollArea vertical scroll policy is set to Qt::ScrollBarAsNeeded so logically it should only show a bar if the page is larger than the viewable area.

Here's what I already tried to fix this:

  1. Setting the scroll policy to Qt::ScrollBarAlwaysOff. While this gets rid of the scrollbar, its unacceptable since it doesn't let the user know they need to scroll on long pages.

  2. Setting the Minimum/Maximum heights for the QStackedwidget. This makes the scrollbar go away if I set it for a low enough value, but it is unacceptable since it causes some of the widgets to have a squished appearance.

I know the problem has something to do with the QStackedWidget but since this is the first time I've used QStackedWidget I’m not sure what it is. I've also noticed the scroll is always for the same amount; i.e. the scrollable area is always the same size no matter how large/small the page widget is. For some reason, it's slightly larger than the longest page. At first I thought the vertical spacers I put at at the bottom of each page to tighten up the layout were causing this, but taking them out didn't fix it.

Update: Here's the code that controls the Stackedwidget:

void Newconfig::on_Categories_currentItemChanged(QTreeWidgetItem *current)
{

    QModelIndex index=ui->Categories->currentIndex();
    int idx=index.row();

    QString category=current->text(0);

    this->setWindowTitle("Preferences -- " + category);

    if (stack->currentWidget() != 0) {
        stack->currentWidget()->setSizePolicy(QSizePolicy::Ignored,
                                                    QSizePolicy::Ignored);
    }
    stack->setCurrentIndex(idx);
    stack->currentWidget()->setSizePolicy(QSizePolicy::Expanding,
                                                QSizePolicy::Expanding);
    adjustSize();

}

1 Answer 1

5

The QStackedWidget takes the size of the largest widget added to it. In your case, the largest page in your preferences dialog is what is influencing the size of the QStackedWidget and is thus forcing the scroll area to show its scroll bar, even when it doesn't appear to be necessary.

To get around this, you can create a slot that is triggered right before you change the current widget in the QStackedWidget that sets the size policy of the page you are leaving to QSizePolicy::Ignored and the size policy of the page you are about to show to whatever you desire for that page -- QSizePolicy::Expanding for instance. An example of this technique is detailed on this page. There's a lot of code in the example, but the important part is here:

void changeCurrent(int idx)
{
    if (stackWidget->currentWidget() !=0) {
        stackWidget->currentWidget()->setSizePolicy(QSizePolicy::Ignored,
                                                    QSizePolicy::Ignored);
    }
    stackWidget->setCurrentIndex(idx);
    stackWidget->currentWidget()->setSizePolicy(QSizePolicy::Expanding,
                                                QSizePolicy::Expanding);
    adjustSize();
}

This function is called to change the current index on the QStackedWidget. It causes the page that was just being viewed to have a size policy that has no influence on the overall size of the QStackedWidget and the page that is about to be viewed to have a size policy of consequence.

2
  • I've had time to implement this. It works but it's still glitchy. Often the fix doesn't kick in until I’ve clicked several items that don't require a scrollbar.
    – Will Kraft
    Sep 24, 2012 at 21:07
  • @WillKraft That might be because you probably have to initialize all of the widgets within the stacked widget to have an Ignored size policy (except for the initially visible one) when you first put together the stacked widget.
    – RA.
    Sep 24, 2012 at 21:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.