QScrollArea缺少滚动条 [英] QScrollArea missing Scrollbar
问题描述
我认为这与以下问题相同: QScrollArea调整QWidget的大小
I think it is the same problem as : QScrollArea resizing QWidget
但是没有解决方案.让我来揭露问题所在.
but there are not solution. so let me expose the problem.
-
从QWidget继承的
- test 2:
- 组成:
- QSpinBox的向量
- QScrollArea
- QVBoxLayout
- test 2 inherited from QWidget:
- composed :
- vector of QSpinBox
- QScrollArea
- QVBoxLayout
- 没有滚动条
- [已修复]滚动条的内部缩小到适合的空间,因此几乎无法读取任何内容(在执行过程中可以调整窗口的大小,这将导致内部变大并可读,但是不会出现滚动条) >
- There are no scrollbar
- [FIXED] The inside of the scrollbar is shrinked to fit so little space nothing can be read (the window can be resized during execution that will cause the inside to get bigger and be readable nevertheless no scrollbar will appear)
I Think problems come from a single source :: Size Hints and Layouts (http://qt-project.org/doc/qt-5.1/qtwidgets/qscrollarea.html#details)
第二个问题(缩小的窗口小部件)可以通过设置"c-> setSizeConstraint(QLayout :: SetMinimumSize);"来解决
The second problem (shrinked widget) can be solved by setting "c->setSizeConstraint(QLayout::SetMinimumSize);"
我目前正在寻找一种解决方案,用于缺少滚动条
I am currently seeking a solution for the missing scrollbar
这是显示我的问题的代码:
here is a code showing my problem :
<c++> #include <QWidget> #include <QScrollArea> #include <QVBoxLayout> #include <QSpinBox> class test2 : public QWidget { Q_OBJECT public: test2(QWidget *parent = 0) :QWidget(parent) { b = new QScrollArea(this); c = new QVBoxLayout; for (int i = 0; i < 10; i++) { a.push_back(new QSpinBox()); c->addWidget(a[i]); } c->setSizeConstraint(QLayout::SetMinimumSize); b->setLayout(c); b->resize(200, 200); } ~test2() { for (int i = 0; i < 10; i++) delete a[i]; } protected: QVector<QSpinBox*> a; QScrollArea* b; QVBoxLayout* c; }; int main(int argc, char *argv[]) { ///* QApplication app(argc, argv); test2 a; a.show(); return app.exec();//*/ }
:在这里找到解决方案: http://qt-project.org/forums/viewthread/295
EDIT :: found a Solution here: http://qt-project.org/forums/viewthread/295
如果您不想在这里阅读大量无用的代码,请执行以下操作: 他扭曲了小部件中的布局
if you don't want to read huge amount of useless code here what he has done :: he warped the layout inside a widget
解决方案::从ScrollBar继承对象<-小部件<-布局
Solution :: inherit the Object from ScrollBar <- Widget <- Layout
代替小部件<-滚动条<-布局
instead of widget <- ScrollBar <- Layout
但这不是真正的解决方案... 我将尝试给出的示例.
but it a work around not really a solution... I going to try on the example I gave.
有效.有谁有更好的解决方案?
it works. Does anyone have a better solution ??
推荐答案
您不想在滚动区域本身上设置布局.您引用的答案源于对此的误解.
You do not want to set the layout on the scroll area itself. The answer you cite stems from misunderstanding this.
-
您需要在滚动区域内有一个小部件,然后使用
QScrollArea::setWidget
将该小部件传递到该区域.如果滚动区域内只有一个没有子代的小部件,则不需要其他布局.
You need to have a widget within a scrollarea, and you pass that widget to the area using
QScrollArea::setWidget
. If all you have inside the scroll area is one widget with no children, then you don't need additional layout.
您不需要手动跟踪布局所拥有的窗口小部件.具有布局的小部件被删除后,它们将被自动删除.
You do not need to manually keep track of widgets that are owned by a layout. They'll be deleted automatically once the widget that has the layout is deleted.
QScrollArea
小部件未布置在其随附的小部件内.The
QScrollArea
widget is not laid out within its enclosing widget.以下是如何执行此操作的示例:
Below is a working example of how to do it:
// https://github.com/KubaO/stackoverflown/tree/master/questions/scroll-18703286 #include <QScrollArea> #include <QVBoxLayout> #include <QSpinBox> #include <QApplication> class Window : public QWidget { QVBoxLayout m_layout{this}; QScrollArea m_area; QWidget m_contents; QVBoxLayout m_contentsLayout{&m_contents}; QSpinBox m_spinBoxes[10]; public: Window(QWidget *parent = {}) : QWidget(parent) { m_layout.addWidget(&m_area); m_area.setWidget(&m_contents); for (auto & spinbox : m_spinBoxes) m_contentsLayout.addWidget(&spinbox); m_contentsLayout.setSizeConstraint(QLayout::SetMinimumSize); } }; int main(int argc, char *argv[]) { QApplication app(argc, argv); Window w; w.show(); return app.exec(); }
这篇关于QScrollArea缺少滚动条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
- composed :
- 组成: