PDA

View Full Version : Qt3 to Qt4 transition, setWindowIcon() problems



watashi16
6th December 2007, 01:22
I'm making the transition from Qt 3 to Qt 4.3.2 and I'm having trouble with QMainWindow's icon (the icon in the upper left corner in the Windows title bar and in the Windows taskbar) being displayed correctly. I'm using Exceed as a window manager and it's not displaying the colors correctly - the icon's shape is right but the colors are the background and foreground colors chosen in Exceed (and not those of the icon itself).

I'm bringing this up here because this all worked correctly in Qt 3.3. I'm not very familiar with Qt's drawing internals - does anyone know what they changed in the way they set the main window icon between Qt 3 and Qt 4 that might cause this? (Maybe an endian issue...?)

I know I'm grasping at straws here, but any hints would be greatly appreciated. The people at Exceed are saying it's a Qt issue, so I'm SOL until I can (hopefully) bring something back to them. Thanks for your time.

YuriyRusinov
6th December 2007, 08:15
There is another mechanism of initialization widgets in Qt4. Could you post problem codes of initialization of QMainWindow ?

watashi16
6th December 2007, 16:54
This code will cause the problems that I'm seeing:

main.cpp:


#include <qapplication.h>
#include "testUi.h"

int main( int argc, char ** argv )
{
QApplication app( argc, argv );
QMainWindow mw;
Ui::TestUi ui;
ui.setupUi( &mw );
mw.show();
return app.exec();
}

testUi.h:


namespace Ui {

class TestUi
{
public:
QWidget *centralwidget;
QMenuBar *menubar;
QStatusBar *statusbar;

void setupUi(QMainWindow *MainWindow)
{
if (MainWindow->objectName().isEmpty())
MainWindow->setObjectName(QString::fromUtf8("MainWindow"));
MainWindow->resize(469, 233);
MainWindow->setWindowIcon(QIcon(QString::fromUtf8("images/green.png")));
centralwidget = new QWidget(MainWindow);
centralwidget->setObjectName(QString::fromUtf8("centralwidget"));
MainWindow->setCentralWidget(centralwidget);
menubar = new QMenuBar(MainWindow);
menubar->setObjectName(QString::fromUtf8("menubar"));
menubar->setGeometry(QRect(0, 0, 469, 30));
MainWindow->setMenuBar(menubar);
statusbar = new QStatusBar(MainWindow);
statusbar->setObjectName(QString::fromUtf8("statusbar"));
MainWindow->setStatusBar(statusbar);

retranslateUi(MainWindow);

QMetaObject::connectSlotsByName(MainWindow);
} // setupUi

void retranslateUi(QMainWindow *MainWindow)
{
MainWindow->setWindowTitle(QApplication::translate("MainWindow", "MainWindow", 0, QApplication::UnicodeUTF8));
Q_UNUSED(MainWindow);
} // retranslateUi

};

}

green.png is a 32-bit png.

YuriyRusinov
7th December 2007, 11:16
Possible you have to write something like this
file test.h


#ifndef __test_h
#define __test_h

namespace Ui {
class test;
};

class YourWindow : public QMainWindow
{
Q_OBJECT
public:
YourWindow (QWidget *parent=0, Qt::WFlags f=0);
...
private:
Ui::test *UI;
...
};

test.cpp



#include "ui_test.h"

#include "test.h"

YourWindow :: YourWindow (QWidget *parent, Qt::WFlags f) :
QMainWindow (parent, f)
{
UI = new Ui::test;
UI->setup (this);
...
}

...

file main.cpp



#include <QApplication>
#include "test.h"

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

form.show ();
return app.exec ();
}

wysota
7th December 2007, 12:39
You are using a relative path to the image. It might be that the application is started from a path other than you think and the image doesn't get displayed. Either make sure the path is correct (for example by changing to an absolute path or by changing the current directory to the applicationDir() before you create the widget) or use Qt resource system (see docs for details) to embed the icon within your binary.

watashi16
13th December 2007, 20:18
All - thanks for your help. It turns out that since Qt has started enforcing rules in the ICCCM, Hummingbird hasn't had a chance to catch up with the new specs. Because of this, Qt thinks that there's an unknown window manager sitting around, and thus defaults the icon to 1-bit depth, which causes the behavior I was seeing. Outside of modifying the Qt source, there doesn't seem to be a workaround. I'm talking to both Trolltech and Hummingbird about the problem. Thanks again!