PDA

View Full Version : QIcon not showing up in QAction in Linux



lni
4th August 2012, 10:53
Hi,

I have tried Qt 4.6.2, 4.8.0, and 4.8.1, none of these versions will show QIcon in the QAction. However, Qt 5.0.0 alpha will show the icon.

The QIcon shows up in QWidgetAction correctly however (see example). This only occurs in Linux. Windows is working fine.

Can someone please take a look at the code and see what is wrong? You can try any icon and rename it to pick.png at same directory.

Many thanks!

PS. I also run Qt's examples, the QIcon does not show up either, such as examples/network/torrent



#include <QApplication>
#include <QContextMenuEvent>
#include <QWidgetAction>
#include <QMenu>
#include <QPushButton>
#include <QDebug>

class MyWidgetAction : public QWidgetAction {

public:

MyWidgetAction( QObject* parent ) :
QWidgetAction( parent ) {

QPixmap pixmap( "./pick.png" );
QPushButton* w = new QPushButton( QIcon( pixmap ), "" );

setDefaultWidget( w );
}

};

class MyWidget : public QWidget {

protected:

virtual void contextMenuEvent ( QContextMenuEvent * event ) {

QMenu* menu = new QMenu;
menu->setAttribute( Qt::WA_DeleteOnClose, true );

QIcon icon( QPixmap( "./pick.png" ) );
QAction* action = new QAction( icon, "Test", this );
menu->addAction( action );

MyWidgetAction* wact = new MyWidgetAction( this );
menu->addAction( wact );
connect( wact->defaultWidget(), SIGNAL( clicked() ),
menu, SLOT( hide() ) );

menu->popup( event->globalPos() );
}

};

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

MyWidget w;
w.resize( 300, 300 );
w.show();

return app.exec();

}



Added after 33 minutes:

I just discover that if I do the following, the QIcon shows up, but all Qt application's windows look werid. Any idea?

unsetenv GNOME_DESKTOP_SESSION_ID DESKTOP_SESSION

roxton
5th August 2012, 13:21
Can you check is PNG image loaded? (i.e. pixmap.isEmpty)
Also try QAction* action = new QAction (QIcon ("path"), "Test", this );

ecanela
5th August 2012, 20:09
you are using a relative path to the icon (./pick.png), this path is relative to the exec file, not the dir where the source code are. use the function QString QDir::currentPath () to knows what are the current path using bu the app.

a better alternative are using qt4 resources systems to store wht images. ( http://doc.trolltech.com/4.0/resources.html)

weixianlee
28th March 2017, 16:41
I have tried setting it up in the .ui interface through the Action Editor,


myAction -> Edit... -> Icon: -> on the arrow, Choose File... -> myIcon.png -> OK

It does load up in the preview above, but does not load during the app run. Is this normal for Linux?