0

これはAPI ドキュメントです:

使い方がわからないのですが、どんな効果があるのでしょうか?次のようにテストしたコード:

#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);ん、助けてくれませんか、ありがとう!!

4

1 に答える 1

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();
}
于 2012-04-29T03:29:47.963 に答える