Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ qscrollara:由垂直滚动条引起的水平滚动条_C++_Qt_Qscrollarea - Fatal编程技术网

C++ qscrollara:由垂直滚动条引起的水平滚动条

C++ qscrollara:由垂直滚动条引起的水平滚动条,c++,qt,qscrollarea,C++,Qt,Qscrollarea,我有一个简单的对话框,里面有一个QScrollArea: // Vertical container for the dialog QVBoxLayout *cont = new QVBoxLayout; this->setLayout(cont); //"this" is my derived QDialog class // ScrollArea for iconFrame QScrollArea *scroll = new QScrollArea; cont->insertW

我有一个简单的对话框,里面有一个QScrollArea:

// Vertical container for the dialog
QVBoxLayout *cont = new QVBoxLayout;
this->setLayout(cont); //"this" is my derived QDialog class

// ScrollArea for iconFrame
QScrollArea *scroll = new QScrollArea;
cont->insertWidget(0, scroll );

// The frame to be added to the QScrollArea
QFrame *iconFrame = new QFrame;
scroll->setWidget(iconFrame);
scroll->setWidgetResizable(true);

// Grid layout for iconFrame
QGridLayout *grid = new QGridLayout;
iconFrame->setLayout(grid);

// Second child widget for the dialog
QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok|QDialogButtonBox::Cancel);
cont->insertWidget(1, buttonBox);

int maxcol = int(ceil(sqrt(numberOfButtons)));
if(maxcol > 6) maxcol = 6;
for(int i=0; i<numberOfButtons; i++)
{
    QPushButton *button= new QPushButton("My Button");
    button->setFixedSize(48, 48);

    int row = int(floor(i/maxcol));
    grid->addWidget(button, row, i-row*maxcol);
}
//对话框的垂直容器
QVBoxLayout*cont=新的QVBoxLayout;
此->设置布局(续)//“this”是我的派生QDialog类
//用于iconFrame的滚动区域
QScrollArea*滚动=新QScrollArea;
cont->insertWidget(0,滚动);
//要添加到QScrollArea的帧
QFrame*iconFrame=新QFrame;
滚动->设置小部件(iconFrame);
滚动->设置WidgetResizeable(真);
//iconFrame的网格布局
QGridLayout*grid=新的QGridLayout;
iconFrame->setLayout(网格);
//对话框的第二个子部件
QDialogButtonBox*buttonBox=新的QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
cont->insertWidget(1,按钮盒);
int maxcol=int(ceil(sqrt(numberOfButtons));
如果(maxcol>6)maxcol=6;
for(int i=0;isetFixedSize(48,48);
int row=int(楼层(i/maxcol));
网格->添加小部件(按钮、行、i行*maxcol);
}
由于最多有6列,因此框架和对话框垂直增长

它的工作原理与预期一样,只是由于垂直滚动条增加了宽度,才绘制水平滚动条

我尝试了不同的SizePolicys和SizeConstraint组合,但似乎没有任何效果


如何消除水平滚动条?

您可以使用滚动条策略抑制滚动条:

scroll->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
请注意,内容可能会被剪裁

对于滚动区域有更多可用空间的情况,您可以尝试
widgetsizeable

scroll->setWidgetResizable(true);

与其说它是一种解决方案,不如说它是一种变通方法,但它确实有效

iconFrame->adjustSize(); // Only necessary when contents of iconFrame have changed
if(iconFrame->height() > scroll->height())
    scroll->setMinimumWidth(iconFrame->width()+scroll->verticalScrollBar()->height());

唯一的方法是确保您的窗口内容宽度小于总宽度减去Vscrollbar宽度。Qt无法轻松确定这几个像素中是否有重要内容,例如右对齐UI元素的打开或关闭小部件。您是否编译了此代码?是的,但这只是一个片段。如果我使用它,c内容确实被剪裁了。隐藏在垂直滚动条后面的QFrame部分仍将被隐藏。我不确定这在这种情况下是否可以接受,但如果我没有找到其他解决方案,我会记住它。谢谢。检查我的更新。这可能对你的情况有所帮助。否则:没有足够的水平空间容纳内容。那么你能做什么n除了剪辑或显示滚动条外,您还能做什么?您可以从一开始就给它更多的空间,以解释最终的垂直滚动条…
setwidgetsizeable
已启用(如OP中所示)。但您的评论让我想到只需手动设置对话框的最小宽度。我将发布一个答案。