使用qtcreator在qt小部件中插入滚动条 [英] Insert a scrollbar in a qt widget using qtcreator

查看:603
本文介绍了使用qtcreator在qt小部件中插入滚动条的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了自己的窗口小部件,然后将其从代码放入另一个窗口小部件中.表单小部件是使用qtCreator创建的.我已经将"subwidgets"安排到了verticalLayout中,以便将一个放置在另一个下方,但是由于插入的小部件的数量不是固定的,因此我需要一个滚动条. 所以我将我的verticalLayout放在了一个scrollArea中,但是滚动条却从未出现过,为什么?结构父亲->孩子"如下: formWidget-> scrollArea-> verticalLayout 谢谢

I have created my own widget that I'm going to put into another widget from code. The form widget are created with qtCreator. I have arranged the "subwidgets" into a verticalLayout in order to put one under the other, but since the number of the inserted widget is not fixed I need to have a scrollbar. So I have placed my verticalLayout into a scrollArea, but the scrollbar never appears, why? the structure "father->child" is the following: formWidget->scrollArea->verticalLayout Thanks

问题如下:我正在从QPushButton的单击插槽中创建窗口小部件: 在该小部件的构造函数中,我正在创建QScrollArea和VerticalLayout. 在将所有想要的小部件插入布局后,将布局添加到scrollArea. 这是错误的事情:为了使通过按钮创建的窗口小部件中的scrollArea可见,必须直接从单击的插槽代码中将该窗口小部件插入滚动条中. 有关更多详细信息,请附加点击的广告位和小部件构造函数的代码

The problem is the following: I was creating the widget from a the clicked slot of a QPushButton: in the costructor of that widget, I was creating a QScrollArea and a VerticalLayout. After having been inserted all the widgets that I want in the layout, I added the layout to the scrollArea. This is the wrong thing: in order to make visible the scrollArea in the widget created from the button, is necessary to inserted that widget in the scrollbar, directly from the clicked slot code. For further detail, I attach the code of the clicked slot and the widget constructor

按钮槽(单击)

scrollArea= new QScrollArea;
scheduleWindow = new Schedule(traceFilePath);
scrollArea->setWidget(scheduleWindow);
scrollArea->resize(scheduleWindow->getWidth(), scheduleWindow->getHeight());
QRect rec = QApplication::desktop()->screenGeometry();
unsigned int desktopHeight = rec.height();
unsigned int desktopWidth = rec.width();
if(scheduleWindow->getWidth() > desktopWidth ||
   scheduleWindow->getHeight() > desktopHeight)
    scrollArea->showMaximized();
else
    scrollArea->show();

小部件构造器

Schedule::Schedule(QString pathname, QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Schedule)
{
     ui->setupUi(this);

    traceParser parser(pathname);
    parser.readJson();
    ArchitectureParameter arch = parser.getArchParam();

    QString taskName;

    unsigned int nTasks = 0;
    TaskSchedule *t;
    for(std::list<QString>::iterator taskNameIter = parser.getTaskNames().begin();
        taskNameIter != parser.getTaskNames().end(); taskNameIter++)
    {
        taskName = *taskNameIter;
        nTasks++;
        cout<<taskName.toStdString()<<endl;
        t = new TaskSchedule(this , taskName, 80, arch.nCPU(), arch.maxTime(),
                                          parser.getExecList(taskName), parser.getTaskSimpleEventsMap(taskName));
        ui->pageLayout->addWidget(t);
    }
    cout<<nTasks<<endl;
    width = 2*t->getLineXPosStart() + t->getTickLength()*arch.maxTime();
    height = nTasks*(2*TASKSCH_Y_OFFSET + arch.nCPU()*t->getCpuHeight());
    ui->area->resize(width, height);
    ui->area->setMinimumSize(width, height);
    this->adjustSize();

} 

推荐答案

您必须像您所说的那样放置一个scrollArea. 然后将要滚动"的所有项目拖到该区域内. 一旦它们位于scrollArea内,您应该选择它并设置垂直或水平布局. 然后,当您调整窗口小部件的大小时,在需要时会出现scrollBar.

You must place a scrollArea, just like you said. And then drag all the items you want to be "scrollable" inside that area. Once they are inside the scrollArea, you should select it and set a layout, vertical or horizontal. And then, when you resize the widget, the scrollBar appears when needed.

请给我一些照片,为您介绍一下.这样我们就可以理解问题了吗? 似乎是一个图形问题,因此如果没有其他内容,很难指导您...

Could you please maybe ilustrate us with some pictures. So we could understand the problem? Seems like a graphical problem, so it's hard to guide you without something to look at...

我相信,为了重新排列布局中的子部件",首先必须定义布局本身的这些子部件"的父级.

I believe in order to rearrange "subwidgets" in a layout, first you must define the parent of these "subwidgets" the layout itself.

这篇关于使用qtcreator在qt小部件中插入滚动条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆