0

如果单击按钮,我希望在同一窗口中显示图像。但是找不到图片,不管是在同一个目录还是我写下整个路径。我看到了几种整合图片的方法,并认为这是最好的一种:

MyApp::MyApp(QWidget *parent)
    : QWidget(parent)
{
    QPushButton *button = new QPushButton("&Randomize!");
    gitterlayout->addWidget(button, 5, 0);

    connect(button, SIGNAL(clicked()), this, SLOT(Randomizer()));
}

void MyApp::Randomizer()
{

    QLabel* bild = new QLabel;
    QPixmap pixmap("question.png"); // the important part, the picture is in my project folder
    if(!pixmap.isNull())
    {
        bild->setPixmap(pixmap);
        bild->show();
    }
    else
    {
        qDebug() << "Cannot find picture"; // I always get this message
    }

    /* I want this question image to appear because once you click on the button, 
       a text will be copied to the clipboard and you can't see it until you paste it 
       (I left it out for this topic) */
}

如果我使用QPixmap pixmap("C:/myproject/question.png");(完整路径)它也不起作用。

4

1 回答 1

1

因为 Qt 默认使用构建文件夹(在这种情况下为“C:\\build-myproject-5.4.2-debug”)作为工作文件夹(如果您在发布模式下运行,则为 build-myProject-5.4.2-release) . 您可以通过切换到项目模式和 balabala 来更改它。或者只是将您的图片放入该工作文件夹。在windows上,路径分隔符应该是'\'而不是'/',这通常会带来很多麻烦。

通过 Qt creator 运行时,您的所有资源都应该放在这里。

于 2015-07-18T12:28:35.803 回答