我正在学习如何使用Qt进行C++编程。当我点击一个按钮,并且该按钮在矩阵上的位置等于-1时,我有一个想要显示的图像,我还想清除该按钮上的文本,到目前为止,我的代码是:
if(Tabuleiro[x][y] == -1){
this->Botoes[x][y]->setText("");
this->Botoes[x][y]->setIcon(QIcon("bomba.png"));
}因为Tabuleiro是一个整数矩阵,所以Botoes是一个指向QPushButtons的指针矩阵,而"bomba.png“是我想要显示的图像。该图像与项目位于同一文件夹中,但是一旦我运行它,它就不会显示。我也试过使用Qt资源系统,我在上面创建了一个名为imagens.qrc的新资源,我创建了一个前缀/Imagem,并将我的图像放在那里,这是代码在此之后的样子:
if(Tabuleiro[x][y] == -1){
this->Botoes[x][y]->setText("");
this->Botoes[x][y]->setIcon(QIcon(":/Imagem/bomba.png"));
}但它仍然不会起作用。我做错了什么?此外,我还尝试使用
this->Botoes[x][y]->text().clear();而不是
this->Botoes[x][y]->setText("");但是它没有起作用,你知道为什么吗?
发布于 2015-11-26 00:06:38
请包含<QApplication>和<QStyle>,然后尝试:
this->Botoes[x][y]->setIcon( qApp->style()->standardIcon( QStyle::SP_MessageBoxWarning ) );如果它工作(显示警告图标):那么,这意味着你没有正确加载你的资源。
您还可以检查以下内容:
QPixmap foo( ":/Imagem/bomba.png" );
bool found = !foo.isNull(); // true if png file was found, false if it was not如果为false,则再次表明您没有正确加载资源;如果为true,则图标应显示在按钮中。
此外,你也可以尝试this->Botoes[x][y]->setIconSize( QSize(16,16) ),因为如果之前有人做了this->Botoes[x][y]->setIconSize( QSize(0,0) );,你的按钮图标将不会显示!
https://stackoverflow.com/questions/33919351
复制相似问题