Results 1 to 8 of 8

Thread: Subclassing QMainWindow problem

  1. #1
    Join Date
    Apr 2009
    Posts
    3
    Thanks
    1

    Default Subclassing QMainWindow problem

    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

    Qt Code:
    1. #ifndef FREECELL_H
    2.  
    3. #define FREECELL_H
    4.  
    5. #include <QMainWindow>
    6.  
    7. class QWidget;
    8.  
    9.  
    10.  
    11.  
    12.  
    13. class FreeCell : public QMainWindow
    14.  
    15. {
    16.  
    17. Q_OBJECT
    18.  
    19. public:
    20.  
    21. FreeCell();
    22.  
    23. protected:
    24.  
    25. void closeEvent(QCloseEvent *event);
    26.  
    27. private slots:
    28.  
    29. void newGame();
    30.  
    31. private:
    32.  
    33. void createActions();
    34.  
    35. void createMenus();
    36.  
    37. void createCentral();
    38.  
    39. QAction *newGameAction;
    40.  
    41. QAction *exitAction;
    42.  
    43. QAction *aboutAction;
    44.  
    45. QMenu *fileMenu;
    46.  
    47. QMenu *helpMenu;
    48.  
    49. QGraphicsScene *centralScene;
    50.  
    51. QGraphicsView *centralView;
    52.  
    53. };
    54.  
    55. #endif // FREECELL_H
    To copy to clipboard, switch view to plain text mode 

    .cpp

    Qt Code:
    1. #include "freecell.h"
    2.  
    3. #include <QtGui>
    4.  
    5.  
    6. FreeCell::FreeCell()
    7.  
    8. {
    9.  
    10. createActions();
    11.  
    12. createMenus();
    13.  
    14. createCentral();
    15.  
    16. }
    17.  
    18.  
    19. void FreeCell::closeEvent(QCloseEvent *event)
    20.  
    21. {
    22.  
    23. int cod = QMessageBox::warning(this, tr("FreeCell"),
    24.  
    25. tr("Você tem certeza que deseja sair?"),
    26.  
    27. QMessageBox::Yes, QMessageBox::No | QMessageBox::Default);
    28.  
    29. if( cod == QMessageBox::Yes )
    30.  
    31. event->accept();
    32.  
    33. else
    34.  
    35. event->ignore();
    36.  
    37. }
    38.  
    39.  
    40. void FreeCell::createActions()
    41.  
    42. {
    43.  
    44. newGameAction = new QAction(tr("&Novo Jogo"), this);
    45.  
    46. newGameAction->setShortcut(tr("Ctrl + N"));
    47.  
    48. newGameAction->setStatusTip(tr("Inicia uma nova partida"));
    49.  
    50. connect(newGameAction, SIGNAL(triggered()), this, SLOT(newGame()));
    51.  
    52. exitAction = new QAction(tr("&Sair"), this);
    53.  
    54. exitAction->setStatusTip(tr("Fecha o jogo"));
    55.  
    56. connect(exitAction, SIGNAL(triggered()), this, SLOT(close()));
    57.  
    58. aboutAction = new QAction(tr("&Sobre"), this);
    59.  
    60. aboutAction->setStatusTip(tr("Informaçoes sobre o programa"));
    61.  
    62. connect(aboutAction, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
    63.  
    64. }
    65.  
    66.  
    67. void FreeCell::createMenus()
    68.  
    69. {
    70.  
    71. fileMenu = menuBar()->addMenu(tr("Arquivo"));
    72.  
    73. fileMenu->addAction(newGameAction);
    74.  
    75. fileMenu->addSeparator();
    76.  
    77. fileMenu->addAction(exitAction);
    78.  
    79. helpMenu = menuBar()->addMenu(tr("Ajuda"));
    80.  
    81. helpMenu->addAction(aboutAction);
    82.  
    83. }
    84.  
    85.  
    86. void FreeCell::createCentral()
    87.  
    88. {
    89.  
    90. centralScene = new QGraphicsScene(this);
    91.  
    92. centralView = new QGraphicsView;
    93.  
    94. //adição de todas as widgets e outros
    95.  
    96. centralScene->addPixmap(QPixmap(":/images/spider3.jpg"));
    97.  
    98. centralScene->setBackgroundBrush(Qt::darkGreen);
    99.  
    100. centralScene->update(centralScene->sceneRect());
    101.  
    102. centralView->setScene(centralScene);
    103.  
    104. setCentralWidget(centralView);
    105.  
    106. centralView->show();
    107.  
    108. }
    109.  
    110.  
    111. void FreeCell::newGame()
    112.  
    113. {
    114.  
    115. }
    To copy to clipboard, switch view to plain text mode 

    main

    Qt Code:
    1. #include "freecell.h"
    2.  
    3. #include <QApplication>
    4.  
    5.  
    6. int main(int argc, char *argv[])
    7.  
    8. {
    9.  
    10. QApplication app(argc, argv);
    11.  
    12. FreeCell *game = new FreeCell;
    13.  
    14. game->show();
    15.  
    16. return app.exec();
    17.  
    18. }
    To copy to clipboard, switch view to plain text mode 

    .qrc code
    Qt Code:
    1. <RCC>
    2. <!DOCTYPE RCC><RCC version="1.0">
    3. <qresource>
    4. <file>images/solidSnake.jpg</file>
    5. <file>images/spider3.jpg</file>
    6. </qresource>
    7. </RCC>
    To copy to clipboard, switch view to plain text mode 

    Thanks in advance
    Last edited by lerwys; 27th April 2009 at 14:26. Reason: .qrc source code added

  2. #2
    Join Date
    Apr 2008
    Posts
    104
    Thanks
    8
    Thanked 7 Times in 7 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Subclassing QMainWindow problem

    Probably "spider3.jpg" is not "solidSnake.jpg"

  3. #3
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Thanks
    5
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Subclassing QMainWindow problem

    You forgot Q_INIT_RESOURCE in main.cpp
    A camel can go 14 days without drink,
    I can't!!!

  4. #4
    Join Date
    Apr 2009
    Posts
    3
    Thanks
    1

    Default Re: Subclassing QMainWindow problem

    Quote Originally Posted by roxton View Post
    Probably "spider3.jpg" is not "solidSnake.jpg"
    sorry... I meant spider3.jpg

    Quote Originally Posted by mcosta
    You forgot Q_INIT_RESOURCE in main.cpp
    I changed the source code, but the problem persists.

    Any other suggestions are welcome =]

  5. #5
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Subclassing QMainWindow problem

    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

  6. The following user says thank you to aamer4yu for this useful post:

    lerwys (27th April 2009)

  7. #6
    Join Date
    Apr 2009
    Posts
    3
    Thanks
    1

    Talking Re: Subclassing QMainWindow problem

    Quote Originally Posted by aamer4yu View Post
    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?

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

    Thank YOU!!!

  8. #7
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Subclassing QMainWindow problem

    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 "

  9. #8
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Thanks
    5
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Subclassing QMainWindow problem

    You can check the available image formats supported from your Qt version with QImageReader::supportedImageFormats ()
    A camel can go 14 days without drink,
    I can't!!!

Similar Threads

  1. Replies: 1
    Last Post: 30th December 2008, 13:28
  2. Very strange socket programming problem
    By montylee in forum Qt Programming
    Replies: 5
    Last Post: 11th November 2008, 12:05
  3. deployment problem: msvc++ 2008 Express, Qt 4.4.3
    By vonCZ in forum Qt Programming
    Replies: 7
    Last Post: 10th November 2008, 14:38
  4. Problem in using QHttp with QTimer
    By Ferdous in forum Newbie
    Replies: 2
    Last Post: 6th September 2008, 12:48
  5. Replies: 16
    Last Post: 7th March 2006, 15:57

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.