gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 11:26:43 25 4
gpt4 key购买 nike

(注:我昨天开始学习Qt,问这个之前我搜索了一下。)

在使用 Qt Designer 玩了一会儿之后,我决定制作一个更严肃的程序,全部以编程方式进行。以前,简单的任务看起来..简单,现在,显示一个按钮非常复杂,因为它不会显示。

主要.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);
}

所有这一切之后,我得到一个空窗口。

可能的原因,据我所知:

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

还能是什么?

抱歉这样的菜鸟问题...

最佳答案

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

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

enter image description here

关于c++ - Qt按钮没有出现在主窗口中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26405644/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com