简体   繁体   English

基于QWidget的Qt小部件

[英]Qt widget based on QWidget

I try to create my own widget based in QWidget. 我尝试在QWidget中创建自己的小部件。 In constructor of the class i have: 在类的构造函数中我有:

Square(QWidget *parent = 0, const char *name = 0, WFlags fl = 0);

Square::Square(QWidget *parent = 0, const char *name = 0, WFlags fl)
        : QWidget(parent, name, f)
{
        if (!name)
                setName("Game");
        reset();
        underMouse=false;
}

But i see error: 'WFlags' has not been declared 但我看到错误: 'WFlags'尚未宣布

Now i remade my code: 现在我重新编写代码:

class Square : public QWidget
{
    Q_OBJECT

    public:
        Square(QWidget *parent = 0);
};

and in square.cpp: 在square.cpp中:

Square::Square(QWidget *parent)
        : QWidget(parent)
{
}

But i see error: 但我看到错误:

  • error: undefined reference to `vtable for Square' 错误:未定义引用`vtable for Square'

  • error: collect2: ld returned 1 exit status What's wrong? 错误:collect2:ld返回1退出状态有什么问题? How can i declare constructor of the class based in QWidget? 如何在QWidget中声明类的构造函数?

Thank you. 谢谢。

If you're using Qt4, the compiler is absolutely right. 如果您使用Qt4,编译器是绝对正确的。 WFlags has not been declared. WFlags尚未宣布。 It's Qt::WindowFlags . 这是Qt::WindowFlags Also, you don't need name -- that's a Qt3 thing. 此外,你不需要name - 这是Qt3的事情。

See http://doc.qt.io/archives/4.6/qwidget.html#QWidget 请参阅http://doc.qt.io/archives/4.6/qwidget.html#QWidget

By the way, I never bother to allow passing WindowFlags through my constructors. 顺便说一句,我从不打扰允许通过我的构造函数传递WindowFlags If you look at the standard Qt widgets, they don't either (eg http://doc.qt.io/archives/4.6/qpushbutton.html#QPushButton ). 如果你看一下标准的Qt小部件,它们也不会(例如http://doc.qt.io/archives/4.6/qpushbutton.html#QPushButton )。

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

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