Results 1 to 4 of 4

Thread: Resizing QML and QMainWindow

  1. #1
    Join Date
    Jan 2011
    Posts
    2
    Thanks
    1

    Default Resizing QML and QMainWindow

    Hi,

    I have a qml file which contains a Rectangle Element as the root item. I want this rectangle item to be displayed as the mainwindows central widget and this Rectangle Element should be resized according to the mainwindow. So i did like this,

    Qt Code:
    1. MainWindow::MainWindow(QWidget *parent)
    2. : QMainWindow(parent), ui(new Ui::MainWindow)
    3. {
    4. ui->setupUi(this);
    5.  
    6. QDeclarativeView *view = new QDeclarativeView;
    7. setCentralWidget(view);
    8.  
    9. QDeclarativeEngine *engine = view->engine();
    10.  
    11. QDeclarativeComponent component(engine, QUrl::fromLocalFile("qml_files/screen.qml"));
    12. object = component.create();
    13. view->setScene(new QGraphicsScene);
    14. view->scene()->addItem(qobject_cast<QDeclarativeItem*>(object));
    15. }
    16.  
    17. void MainWindow::resizeEvent(QResizeEvent *)
    18. {
    19. object->setProperty("width", width());
    20. object->setProperty("height", height());
    21. }
    22.  
    23.  
    24. screen.qml
    25.  
    26. Rectangle {
    27. id: window
    28.  
    29. width: 480; height: 360
    30. color: "#282828"
    31. }
    To copy to clipboard, switch view to plain text mode 
    which uses QDeclarativeView, QGraphicsScene, QDeclarativeComponent and QDeclarativeItem, which seems to work. Am i doing it right or a much simpler solution exist

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Resizing QML and QMainWindow

    There is a simpler solution.
    Qt Code:
    1. view->setResizeMode(QDeclarativeView::SizeRootObjectToView);
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. The following 2 users say thank you to wysota for this useful post:

    hungarycoder (11th January 2011), MBex (26th January 2011)

  4. #3
    Join Date
    Jan 2011
    Posts
    2
    Thanks
    1

    Default Re: Resizing QML and QMainWindow

    Thanks Wysota,

    The solution is amazingly simple.

  5. #4
    Join Date
    Jan 2010
    Location
    UK
    Posts
    12
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Resizing QML and QMainWindow

    With one exception (see below) I carefully copied this example, but the automatic resizing does not work (the rest works 100%). Here is my code:
    Qt Code:
    1. MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) {
    2. QDeclarativeView * pCanvas = new QDeclarativeView;
    3. setCentralWidget(pCanvas);
    4.  
    5. QDeclarativeEngine *engine = pCanvas->engine();
    6.  
    7. QDeclarativeComponent component(engine, QUrl::fromLocalFile("test.qml"));
    8. pObject = component.create();
    9. pCanvas->setScene(new QGraphicsScene);
    10. pCanvas->scene()->addItem(qobject_cast<QDeclarativeItem*>(pObject));
    11.  
    12. pCanvas->scene()->setBackgroundBrush(Qt::yellow);
    13. pCanvas->setResizeMode(QDeclarativeView::SizeRootObjectToView);
    14. }
    To copy to clipboard, switch view to plain text mode 
    The part I missed out is the User Interface initialization and setup:
    Qt Code:
    1. ui(new Ui::MainWindow)
    2. and
    3. ui->setupUi(this);
    To copy to clipboard, switch view to plain text mode 
    As a newbie, I cannot see what difference this could make. Any hints gratefully received.

Similar Threads

  1. QMainWindow as a child of QMainWindow
    By Elder Orb in forum Qt Programming
    Replies: 8
    Last Post: 12th February 2011, 22:31
  2. [SOLVED] QMainWindow open a new QMainWindow
    By xeroblast in forum Qt Programming
    Replies: 1
    Last Post: 2nd December 2010, 08:31
  3. Replies: 0
    Last Post: 17th November 2010, 18:07
  4. Replies: 3
    Last Post: 2nd March 2010, 21:58
  5. Prevent from resizing a QMainWindow
    By Flier in forum Qt Tools
    Replies: 5
    Last Post: 14th April 2006, 18: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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.