Scrollbar main window
-
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
-
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.
-
there is written that app don't response a i have to close it by task manager
@Chris-Kawa said:
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.
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
-
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); }
!
-
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.
-
@Chris-Kawa said in Scrollbar main window:
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); }
!
How will we go about if the scrollArea is set up in Designer, and the pushbutton and lineedit placed within it there instead of in code?
I modified your above code like this:
QWidget *contentWidget = new QWidget(); ui->pushButton->setParent(ui->scrollArea); ui->lineEdit->setParent(ui->scrollArea); //(^if I dont set their parent here they dont appear, even though in Designer I have placed them inside scrollArea) ui->scrollArea->setWidget(contentWidget); setCentralWidget(ui->scrollArea);
but I dont see any scrollbar.
-
@MH24
Not sure what you are trying to do here.You use
QWidget::setParent()
, I would never do that, always add to a widget, or more to the point add to its layout.Your scroll area has a single widget,
contentWidget
, but then you seem to be trying to put the button and line edit directly onto the scroll area too.I would expect hierarchy to look like this:
Main Window central widget scroll area content widget layout for content widget in scroll area (horizontal or vertical, vertical here) line edit button table widget
I would also think you can do all this in Designer not just from code, but not certain.
-
@JonB I couldnt go to Designer in my actual app because my content was generated dynamically. What I wrote here was for testing. And yes I did indeed end up using layouts, giving them to contentWidget and made that a widget of scrollArea. Then set scrollArea as central widget. But I had a button whose geometry I couldnt quite set within layouts, so I separately added it to scrollArea through setParent and set its geometry from there.
Thank you