Scrollbar main window



  • i create some gui by qt designer and i want make my window scrollable when it needs to scroll because of limited size of window

    my code :

    @MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {

    QScrollArea *scrollarea = new QScrollArea(this);
    
    scrollarea->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
    scrollarea->setWidgetResizable(false);
    
    scrollarea->setGeometry(0,0,700,600);
    this->setCentralWidget(scrollarea);
    
     ui->setupUi(this);
    
    //setWindowFlags( (windowFlags() | Qt::CustomizeWindowHint) & ~Qt::WindowMaximizeButtonHint);
    setFixedWidth(700);
    setFixedHeight(600);
    

    }@

    where is the problem?


  • Moderators

    You haven't said what the problem is, but from the first look you should move the entire scrollarea code below the ui->setupUi(this); line, because that's where the actual place for the central widget is created.



  • i tried like this but this kind of action make application break down



  • I see two options:

    You create the scroll view inside your .ui file directly, and only do the ui->setupUi(this).

    You make sure your ui has a widget as the base class, and then do the setupUi call on the contents widget of the scrollView instead of on this.

    At the moment, your scrollView and your other widgets from your .ui file kind of clash. Both are on the main window, but they know nothing of each other and have no relationship. You will need to make sure the widgets in your .ui file become a children of the contents widget of the scroll view.


  • Moderators

    You need to be more specific - what is the problem and what does break down mean? crash? shut down? not starting? not showing window? showing something different than you intended (if so, what is it etc.). The more details you give the faster you get the answer.

    setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded) - this is the default so you don't need it
    setWidgetResizable(false) - this is the default so you don't need it
    setGeometry(0,0,700,600) - this has no effect since you're placing scrollarea as a central widget so it will resize to fill that area

    To recap - setupUi needs to be on top of any ui mangling. If your app is failing it's because of some other problem.



  • @MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {

    ui->setupUi(this);
    
    QScrollArea *scrollarea = new QScrollArea(this);
    this->setCentralWidget(scrollarea);
    
    scrollarea->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
    scrollarea->setWidgetResizable(false);
    
    //scrollarea->setGeometry(0,0,700,600);
    
    QObjectList list = this->children();
    for(int i=0;i<list.size();++i)
        list[i]->setParent(scrollarea);
    
    
    //setWindowFlags( (windowFlags() | Qt::CustomizeWindowHint) & ~Qt::WindowMaximizeButtonHint);
    //setFixedWidth(700);
    //setFixedHeight(600);
    

    }@

    i tried like this - doesn't turn on , occurs windows error


  • Moderators

    @
    QObjectList list = this->children();
    for(int i=0;i<list.size();++i)
    list[i]->setParent(scrollarea);
    @
    This makes no sense. You are reparenting everything inside the main window, including the scrollarea to scrollarea?

    Maybe just throw out all of that code(except setupUi) and stick to designer. You can add the scrollarea and anything inside it from there.



  • Indeed, it doesn't make sense. In general, if you say that you get errors, you should post those errors. Those errors contain information on what wend wrong.



  • there is written that app don't response a i have to close it by task manager

    [quote author="Krzysztof Kawa" date="1363093681"]@
    QObjectList list = this->children();
    for(int i=0;i<list.size();++i)
    list[i]->setParent(scrollarea);
    @
    This makes no sense. You are reparenting everything inside the main window, including the scrollarea to scrollarea?

    Maybe just throw out all of that code(except setupUi) and stick to designer. You can add the scrollarea and anything inside it from there.[/quote]

    i assume that jak should ui->setup(this) first , next set central widget as scroll area but how i can set all object from window to join scroll view and furthermore how i can join other object creat dynamically



  • @
    ui->setupUi(this);

    QScrollArea *scrollarea = new QScrollArea(this);
    this->setCentralWidget(scrollarea);
    
    scrollarea->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
    scrollarea->setWidgetResizable(false);
    

    @

    scrollarea overwrite gui created by qt designer


  • Moderators

    Take a look at this example. Maybe it will guide you a little more:
    @
    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);

    QLineEdit*    someControl1 = new QLineEdit("example");
    QPushButton*  someControl2 = new QPushButton("example");
    QTableWidget* someControl3 = new QTableWidget(3,3);
    
    QVBoxLayout* layout = new QVBoxLayout();
    layout->addWidget(someControl1);
    layout->addWidget(someControl2);
    layout->addWidget(someControl3);
    
    QWidget* contentWidget = new QWidget();
    contentWidget->setLayout(layout);
    
    QScrollArea* scrollArea = new QScrollArea();
    scrollArea->setWidget(contentWidget);
    
    setCentralWidget(scrollArea);
    

    }
    @
    !http://img341.imageshack.us/img341/2205/scrollarea.jpg(scroll area example)!



  • thanks for your example :) now i've got na question about layouts - is there any kind of layout that i can manage manually not in vertical or horizontal way ? is there possibility to inherit from qlayout and then do it manually?


  • Moderators

    Windows and controls are usually rectangular so the layouts are too - apart from horizontal and vertical there are also QGridLayout, QStackedLayout and QFormLayout.

    You might of course try to inherit from QLayout and do some crazy stuff. Something like CircleLayout would be fun to implement for sure :) But in most cases the 5 basic Qt layouts are more than enough if properly configured so check if any of them meets your requirements before trying to do it yourself.


Log in to reply
 

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