PDA

View Full Version : Subclassing QMainWindow problem



lerwys
27th April 2009, 12:10
Hi all,

I would like to subclass QMainWindow in order to create a simple application: displyaing an image.

Why does my code not show the spider3.jpg image ?

NOTE: I have already made the .qrc file and pointed to the corrected image path
NOTE 2: newGame() is not yet implemented

.h file



#ifndef FREECELL_H

#define FREECELL_H

#include <QMainWindow>

class QWidget;

class QGraphicsScene;

class QGraphicsView;

class QGraphicsPixmapItem;


class FreeCell : public QMainWindow

{

Q_OBJECT

public:

FreeCell();

protected:

void closeEvent(QCloseEvent *event);

private slots:

void newGame();

private:

void createActions();

void createMenus();

void createCentral();

QAction *newGameAction;

QAction *exitAction;

QAction *aboutAction;

QMenu *fileMenu;

QMenu *helpMenu;

QGraphicsScene *centralScene;

QGraphicsView *centralView;

};

#endif // FREECELL_H


.cpp



#include "freecell.h"

#include <QtGui>


FreeCell::FreeCell()

{

createActions();

createMenus();

createCentral();

}


void FreeCell::closeEvent(QCloseEvent *event)

{

int cod = QMessageBox::warning(this, tr("FreeCell"),

tr("Você tem certeza que deseja sair?"),

QMessageBox::Yes, QMessageBox::No | QMessageBox::Default);

if( cod == QMessageBox::Yes )

event->accept();

else

event->ignore();

}


void FreeCell::createActions()

{

newGameAction = new QAction(tr("&Novo Jogo"), this);

newGameAction->setShortcut(tr("Ctrl + N"));

newGameAction->setStatusTip(tr("Inicia uma nova partida"));

connect(newGameAction, SIGNAL(triggered()), this, SLOT(newGame()));

exitAction = new QAction(tr("&Sair"), this);

exitAction->setStatusTip(tr("Fecha o jogo"));

connect(exitAction, SIGNAL(triggered()), this, SLOT(close()));

aboutAction = new QAction(tr("&Sobre"), this);

aboutAction->setStatusTip(tr("Informaçoes sobre o programa"));

connect(aboutAction, SIGNAL(triggered()), qApp, SLOT(aboutQt()));

}


void FreeCell::createMenus()

{

fileMenu = menuBar()->addMenu(tr("Arquivo"));

fileMenu->addAction(newGameAction);

fileMenu->addSeparator();

fileMenu->addAction(exitAction);

helpMenu = menuBar()->addMenu(tr("Ajuda"));

helpMenu->addAction(aboutAction);

}


void FreeCell::createCentral()

{

centralScene = new QGraphicsScene(this);

centralView = new QGraphicsView;

//adição de todas as widgets e outros

centralScene->addPixmap(QPixmap(":/images/spider3.jpg"));

centralScene->setBackgroundBrush(Qt::darkGreen);

centralScene->update(centralScene->sceneRect());

centralView->setScene(centralScene);

setCentralWidget(centralView);

centralView->show();

}


void FreeCell::newGame()

{

}


main



#include "freecell.h"

#include <QApplication>


int main(int argc, char *argv[])

{

QApplication app(argc, argv);

FreeCell *game = new FreeCell;

game->show();

return app.exec();

}


.qrc code



<RCC>
<!DOCTYPE RCC><RCC version="1.0">
<qresource>
<file>images/solidSnake.jpg</file>
<file>images/spider3.jpg</file>
</qresource>
</RCC>



Thanks in advance

roxton
27th April 2009, 12:58
Probably "spider3.jpg" is not "solidSnake.jpg" ;)

mcosta
27th April 2009, 13:00
You forgot Q_INIT_RESOURCE in main.cpp

lerwys
27th April 2009, 15:22
Probably "spider3.jpg" is not "solidSnake.jpg" ;)

sorry... I meant spider3.jpg



You forgot Q_INIT_RESOURCE in main.cpp


I changed the source code, but the problem persists.

Any other suggestions are welcome =]

aamer4yu
27th April 2009, 20:19
Cant guess problem from code.. seems ok.

One thing,, is ur Qt built with jpeg support ? try some other ".PNG" or ".ico" instead of JPG and see if it works

lerwys
28th April 2009, 00:36
Cant guess problem from code.. seems ok.

One thing,, is ur Qt built with jpeg support ? try some other ".PNG" or ".ico" instead of JPG and see if it works

Thank you aamer4yu... I loaded an .PNG image and it woked perfectly!! How come I didn't tought anything like this? :D:D

But why does my Qt does not support .jpg image format?

Thank YOU!!!

aamer4yu
28th April 2009, 06:17
But why does my Qt does not support .jpg image format?

Because you didnt built Qt with that support !
You need to pass "-qt-libjpeg" argument to configure.exe while configuring Qt. Read "configure -help "

mcosta
28th April 2009, 09:40
You can check the available image formats supported from your Qt version with QImageReader::supportedImageFormats ()