2012-04-29 547 views
0

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

我不知道如何使用它,它会起什么作用?我测试了如下代码:

#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

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(); 
} 
+0

感谢您的帮助.. – 2012-04-29 23:09:19