問題描述
我認為這是與以下相同的問題:QScrollArea resizing QWidget
I think it is the same problem as : QScrollArea resizing QWidget
但沒有解決方案.所以讓我揭露這個問題.
but there are not solution. so let me expose the problem.
- 測試2繼承自QWidget:
- 組成:
- QSpinBox 的向量
- QScrollArea
- QVBoxLayout
- 沒有滾動條
- [FIXED] 滾動條的內部被縮小以適應如此小的空間,無法讀取任何內容(可以在執行期間調整窗口大小,這將導致內部變大且可讀,但不會出現滾動條)
我認為問題來自單一來源 :: 尺寸提示和布局 (http://qt-project.org/doc/qt-5.1/qtwidgets/qscrollarea.html#details)
I Think problems come from a single source :: Size Hints and Layouts (http://qt-project.org/doc/qt-5.1/qtwidgets/qscrollarea.html#details)
第二個問題(縮小的widget)可以通過設置"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繼承Object <- Widget <- Layout
Solution :: inherit the Object from ScrollBar <- Widget <- Layout
代替小部件 <- ScrollBar <- 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 缺少滾動條的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!
- 組成: