如何在Qt中使用QWidget :: scroll(int dx,int dy)?

时间:2012-04-29 02:06:28

标签: qt

这是API document

我不确定如何使用它,它的作用是什么?我测试的代码如下:

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>

class Widget : public QWidget
{
    Q_OBJECT
public:
    explicit Widget(QWidget *parent = 0);

signals:

public slots:

};

#endif // WIDGET_H
  

Widget.cpp

#include "Widget.h"
#include<QPushButton>

Widget::Widget(QWidget *parent) :
    QWidget(parent)
{
    QPushButton* bt = new QPushButton(this);
    this->scroll(20, 0);
}

删除scroll(20, 0);时没有任何变化,你能帮助我吗,谢谢!!

1 个答案:

答案 0 :(得分:1)

QWidget :: scroll()移动小部件在屏幕上绘制已经的像素。这意味着应该在显示窗口小部件后调用该函数。换句话说,不是在构造函数中。考虑这个例子:

header.h

#include <QtGui>

class Widget : public QWidget
{
public:
    Widget(QWidget *parent = 0) : QWidget(parent)
    {
        new QPushButton("Custom Widget", this);
    }
};

class Window : public QDialog
{
    Q_OBJECT
public:
    Window()
    {
        QPushButton *button = new QPushButton("Button", this);
        widget = new Widget(this);
        widget->move(0, 50); // just moving this down the window
        widget->scroll(-20, 0); // does nothing! widget hasn't been drawn yet

        connect(button, SIGNAL(clicked()), this, SLOT(onPushButtonPressed()));
    }

public slots:
    void onPushButtonPressed()
    {
        widget->scroll(-20, 0);
    }

private:
    Widget *widget;
};

的main.cpp

#include "header.h"

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Window w;
    w.show();
    return a.exec();
}