简体   繁体   中英

add scrollbar in qt window?

i have been learning qt from video tutorials here . i have completed 35 videos. So i wanted to create a sample application just to understand qt. i am using the latest version of qt creator.

i added some push-buttons & deleted the menu bar & tool bar in main window.

在此输入图像描述

after running i found that if i resize the window scroll-bars don't appear.

在此输入图像描述

i don't want the push-buttons to resize or move. i am fine with them becoming invisible but i want the scroll-bars so that the user can scroll to the invisible parts. i added a scroll area widget but it doesn't work. i did some coding by searching on internet but that didn't work either. when i increase the size of scroll area upto the window size; after running the window appears blank. maybe the push-buttons go 'below' the scroll-area. how do i change which object should be on 'top'?

also since i am just a beginner, i don't know whats the best approach in designing using qt. should i just put the required widgets using designer & then give their characteristics by coding? or should i do complete designing using designer? or should i abandon designer & do the complete coding? THANK YOU!

edit @Synxis, that didnt help much. I even used the secentralwidget(ui->scrollareacontentswidget); i tried deleting the centralwidget QWidget, but i cant. Any solution?

在此输入图像描述

A QWidget does not have scrollbars, you need to add a zone which will have scrollbars.

Just add a QScrollArea to your window, and put all the widgets in it (buttons, etc...). Don't forget to layout your scroll area to the whole main window.

More precisely, the hierarchy is something like that:

main window central widget (with a layout)
    scroll area
        scroll area widget (with a layout, layout scroll area if you're in the designer)
            your container widget (should be layouted also)
                your buttons, layouts, etc...

( (with a layout) = layout the widget with a layout, but the type of the layout doesn't matter, since only one widget needs to be layouted).

Your container widget should be layouted as you want, spacers can be useful.

Let the QHBoxLayout be the a plain QWidget() object and then set that widget object to be scroll area's widget calling setWidget(). That's all actually needed. or at least sometimes you need calling setWidgetResizable(bool) if extra space can be utilized. Example code follows.

#include <QtGui>

class YourWidget : public QWidget
{
public:
    YourWidget(QWidget* parent=0)
        : QWidget(parent)
    {
        QHBoxLayout* buttonLay = new QHBoxLayout;
        for (int i=0; i<10; ++i) {
            QPushButton* button = new QPushButton(tr("Button%1").arg(i));
            buttonLay->addWidget(button);
        }

        QLabel* dummyBigLabel = new QLabel(tr("DummyBigLabel"));
        dummyBigLabel->setAlignment(Qt::AlignCenter);
        dummyBigLabel->setMinimumSize(400,300);
        dummyBigLabel->setFrameStyle(QFrame::Panel | QFrame::Sunken);

        QVBoxLayout* mainLay = new QVBoxLayout(this);
        mainLay->addLayout(buttonLay);
        mainLay->addWidget(dummyBigLabel);
    }
};

int main(int argc, char** argv)
{
    QApplication app(argc, argv);

QWidget* topLevelWidget = 0;
    YourWidget* editor = new YourWidget;
#ifdef USE_NO_SCROLL_AREA
    topLevelWidget = editor;
#else
    QScrollArea* scroller = new QScrollArea;
    scroller->setWidget(editor);
    topLevelWidget = scroller;
#endif
    topLevelWidget->show();
    topLevelWidget->raise();
    return app.exec();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM