Qt按钮没有出现在主窗口中

时间:2014-10-16 13:25:02

标签: c++ qt

(注意:我昨天开始学习Qt,在问这个之前我做了我的搜索。)

在与Qt Designer玩了一下之后,我决定以编程方式制作一个更严肃的程序。而在此之前,简单的任务看起来很简单,现在,对一个按钮进行显示是一件很复杂的事情,因为它没有显示出来。

的main.cpp

int main(int argc, char * argv[])
{
    QApplication app(argc, argv);

    PixelPeep p;
    p.show();

    return app.exec();
}

pixelpeep.h - 相关部分

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

signals:

public slots:

private:
    QToolBar * toolBar;
    QHBoxLayout * toolbarLayout;
    QToolButton * addButton; // add new image

    QScrollBar * zoomBar;
};

pixelpeep.cpp - 相关部分

PixelPeep::PixelPeep(QWidget *parent) :
    QMainWindow(parent)
{
    resize(600,375);

    toolBar = new QToolBar;

    addButton = new QToolButton;
    addButton->setGeometry(20,20,20,20);

    toolBar->addWidget(addButton);
    toolbarLayout = new QHBoxLayout;
    toolbarLayout->addWidget(addButton);
}

毕竟,我得到一个空窗口。

可能的原因,AFAIK:

    在类构造函数中创建后,
  • 按钮将超出范围 - 这不是这里的情况,因为它是由私有成员addButton动态分配和指针
  • 不在布局中或者大小为0 - 事实并非如此,因为这两个问题都是在代码中解决的

还有什么呢?

对不起这样的菜鸟问题......

1 个答案:

答案 0 :(得分:0)

addToolBar(toolBar);构造函数中调用PixelPeep

您没有在按钮上设置任何图标,因此它将显示为不可见。将鼠标悬停在它上面你会看到它:

enter image description here