Results 1 to 4 of 4

Thread: Stopped widget in a moveable QGraphicsScene

  1. #1
    Join Date
    Apr 2009
    Location
    Valencia (Spain)
    Posts
    245
    Thanks
    38
    Thanked 19 Times in 19 Posts
    Qt products
    Qt4
    Platforms
    Symbian S60

    Default Stopped widget in a moveable QGraphicsScene

    Hello,

    I'm doing a game with items in a QGraphicsScene - QGraphicsView. The scene is the entire level, it's big, so the view is following the main character when it's going outside the boundaries.

    But I want to have a static widget (a button) in the bottom-left part of the screen (view). So the view is focusing in the main character but that widget keeps its position on the screen.

    I though I could achieve that by calculating (each frame) the QGraphicsView position in the screen and place the button in the proper scene coordinate.

    So far I tried with QWidget::pos() and QGraphicsView::sceneRect() but it's no use.

    How can I know the position the view is showing of the scene every frame, or, if so, there is any way to have a QGraphicsPixmap item statically in the view?

    thanks!

  2. #2
    Join Date
    Apr 2009
    Location
    Valencia (Spain)
    Posts
    245
    Thanks
    38
    Thanked 19 Times in 19 Posts
    Qt products
    Qt4
    Platforms
    Symbian S60

    Default Re: Stopped widget in a moveable QGraphicsScene

    So, there is no way to know which part of the QGraphicsScene is showing the QGraphicsView? (sceneRect does not work as expected, it return the whole scene)

  3. #3
    Join Date
    Jan 2011
    Location
    Netherlands
    Posts
    17
    Thanks
    4
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Stopped widget in a moveable QGraphicsScene

    If the button is supposed to be in the bottom-left position of the screen (the qgraphicsview) the whole time, then why don't you just create it on top of the qgraphicsview instead of creating it in the qgraphicsscene?

    You can achieve this very easy. Assuming you have a QMainWindow with according ui class and a QGraphicsView widget (graphicsview) is placed in this form. The code look like:

    mainwindow.h
    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include <QMainWindow>
    5.  
    6. namespace Ui {
    7. class MainWindow;
    8. }
    9.  
    10. class MainWindow : public QMainWindow
    11. {
    12. Q_OBJECT
    13.  
    14. public:
    15. explicit MainWindow(QWidget *parent = 0);
    16. ~MainWindow();
    17.  
    18. void createButton(int buttonWidth, int buttonHeight);
    19.  
    20. private:
    21. Ui::MainWindow *ui;
    22. };
    23.  
    24. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 

    mainwindow.cpp
    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3.  
    4. #include <QPushButton>
    5.  
    6. MainWindow::MainWindow(QWidget *parent) :
    7. QMainWindow(parent),
    8. ui(new Ui::MainWindow)
    9. {
    10. ui->setupUi(this);
    11. }
    12.  
    13. MainWindow::~MainWindow()
    14. {
    15. delete ui;
    16. }
    17.  
    18. void MainWindow::createButton(int buttonWidth, int buttonHeight)
    19. {
    20. QPushButton* button = new QPushButton(ui->graphicsView);
    21. button->setText("Click me!");
    22.  
    23. button->setGeometry(0,
    24. ui->graphicsView->geometry().height() - buttonHeight,
    25. buttonWidth, buttonHeight);
    26.  
    27. button->show();
    28. }
    To copy to clipboard, switch view to plain text mode 

    main.cpp
    Qt Code:
    1. #include <QtGui/QApplication>
    2. #include "mainwindow.h"
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QApplication a(argc, argv);
    7. MainWindow w;
    8. w.show();
    9.  
    10. w.createButton(100,50);
    11.  
    12. return a.exec();
    13. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by Stef; 4th February 2011 at 08:31. Reason: Add example code

  4. The following user says thank you to Stef for this useful post:

    jano_alex_es (4th February 2011)

  5. #4
    Join Date
    Apr 2009
    Location
    Valencia (Spain)
    Posts
    245
    Thanks
    38
    Thanked 19 Times in 19 Posts
    Qt products
    Qt4
    Platforms
    Symbian S60

    Default Re: Stopped widget in a moveable QGraphicsScene

    Lol, I didn't think in that possibility... thanks!

Similar Threads

  1. Replies: 1
    Last Post: 29th July 2010, 05:41
  2. How to draw rectangle with moveable edge
    By sagirahmed in forum Qt Programming
    Replies: 4
    Last Post: 2nd July 2010, 06:21
  3. QResource Stopped Working
    By JPNaude in forum Qt Programming
    Replies: 0
    Last Post: 22nd October 2008, 12:26
  4. Moveable tabs
    By fear in forum KDE Forum
    Replies: 1
    Last Post: 25th March 2007, 19:51
  5. Signals/Slots stopped working
    By Jimmy2775 in forum Qt Programming
    Replies: 8
    Last Post: 31st March 2006, 21:11

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
  •  
Qt is a trademark of The Qt Company.