简体   繁体   English

Qt重新实现功能

[英]Qt reimplement function

I need to subclass qt class and reimplement the virtual function. 我需要对qt类进行子类化并重新实现虚函数。 Say that i have the QLCDNumber class and I want to reimplement the function that set the position of the number when resize the LCD number screen, how to achieve that? 假设我具有QLCDNumber类,并且我想重新实现在调整LCD数字屏幕尺寸时设置数字位置的功能,如何实现?

I read that by inhert a class from QLCDNumber and reimplement the function, but where to get that function code so that I can edit what needed in that function? 我通过从QLCDNumber继承一个类并重新实现该功能来读取该信息,但是在哪里可以获取该功能代码,以便我可以编辑该功能所需的内容? I read the documentation but it explains the function uses and not show its code. 我阅读了文档,但是它解释了该函数的用法,而不显示其代码。 Example image: 示例图片: 范例图片

I mentioned the QLCDNumber class as example, I need to know the prosses of reimplemnt a virtual Qt class function. 我以QLCDNumber类为例,我需要了解重新实现虚拟Qt类函数的过程。

For this particular problem you can use this type of approach where you can create an object which inherits QWidget and implement the paintEvent() of the class like this: 对于此特定问题,您可以使用这种方法,在其中您可以创建一个继承QWidget并实现此类的paintEvent()的对象,如下所示:

class MyWidget : public QWidget
{
    Q_OBJECT
public:
    explicit MyWidget(QWidget *parent = 0);
    ~MyWidget();
protected:
    void paintEvent(QPaintEvent *pe);
signals:
public slots:
};

and in the paintEvent() 并在paintEvent()中

void MyWidget::paintEvent(QPaintEvent *pe)
{
    QPainter *painter = new QPainter(this);
    painter->begin(this);
    // draw what you want here using painter
    // and consider your different layouts
    painter->end();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM