Results 1 to 18 of 18

Thread: qwidget into QML

  1. #1
    Join Date
    Nov 2010
    Posts
    82
    Thanked 9 Times in 9 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default qwidget into QML

    hi all,

    I'm trying to put a treeview in my QML application.
    The last thing i was trying was to put a qwidget (QTreewidget) into my QML file.
    so i used qmlRegisterType, and have :

    javascript Code:
    1. import QtQuick 1.0
    2. import Test 1.0
    3.  
    4. Rectangle {
    5. id:ttt
    6. width: 360
    7. height: 360
    8. CustomTree{
    9. id:customW
    10. parent:ttt
    11. // anchors.fill: parent
    12. }
    13. }
    To copy to clipboard, switch view to plain text mode 

    as my QML file but nothing happens
    can someone help me ?
    Last edited by wysota; 15th August 2011 at 15:52. Reason: missing [code] tags

  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: qwidget into QML

    Did you implement a subclass of QDeclarativeItem for yout widget?
    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. #3
    Join Date
    Nov 2010
    Posts
    82
    Thanked 9 Times in 9 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: qwidget into QML

    this is the part i'm not sure of
    i create an object subclassed from QDeclarativeItem with
    this->setFlag(QGraphicsItem::ItemHasNoContents, false);

    my problem is how to paint it. I'm not familiar with the paint() function

  4. #4
    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: qwidget into QML

    I don't see your point. In my opinion your declarative item should create a tree view, a proxy item for it and set that item as the child of the declarative item. Then you can forward all calls you want from the item to the widget.
    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.


  5. #5
    Join Date
    Nov 2010
    Posts
    82
    Thanked 9 Times in 9 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: qwidget into QML

    i must be really @#?! or tired
    when i try this
    Qt Code:
    1. CustomWidget::CustomWidget(QDeclarativeItem *parent) :
    2. QDeclarativeItem(parent)
    3. {
    4. this->setFlag(QGraphicsItem::ItemHasNoContents, false);
    5.  
    6. tree = new QTreeWidget;
    7. new QTreeWidgetItem(tree, QStringList()<<"First");
    8. new QTreeWidgetItem(tree, QStringList()<<"Second");
    9. new QTreeWidgetItem(tree, QStringList()<<"Third");
    10. new QTreeWidgetItem(tree, QStringList()<<"Fourth");
    11.  
    12. proxy = new QGraphicsProxyWidget;
    13. proxy->setWidget(tree);
    14. proxy->setParentItem(this);
    15.  
    16.  
    17. }
    To copy to clipboard, switch view to plain text mode 

    in the constructor of my QDeclarativeItem it doesn't display anything

    here is the qml file

    Qt Code:
    1. import QtQuick 1.0
    2. import Test 1.0
    3.  
    4. Rectangle {
    5. id:ttt
    6. width: 360
    7. height: 360
    8. CustomTree{
    9. id:customW
    10. anchors.fill:parent
    11. }
    12. }
    To copy to clipboard, switch view to plain text mode 

  6. #6
    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: qwidget into QML

    This works for me:
    Qt Code:
    1. #include <QtDeclarative>
    2. #include <QtGui>
    3.  
    4. class PushButtonItem : public QDeclarativeItem {
    5. Q_OBJECT
    6. public:
    7. PushButtonItem(QDeclarativeItem *parent =0) : QDeclarativeItem(parent) {
    8. pb = new QPushButton("text");
    9. proxy = new QGraphicsProxyWidget(this);
    10. proxy->setWidget(pb);
    11. proxy->setPos(-pb->sizeHint().width()/2, -pb->sizeHint().height()/2);
    12. }
    13. private:
    14. QGraphicsProxyWidget *proxy;
    15. };
    16.  
    17.  
    18. #include "main.moc"
    19.  
    20. int main(int argc, char **argv) {
    21. QApplication app(argc, argv);
    22. QDeclarativeView view;
    23. qmlRegisterType<PushButtonItem>("PushButton", 1, 0, "PushButtonItem");
    24. view.setSource(QUrl::fromLocalFile("file.qml"));
    25. view.show();
    26. return app.exec();
    27. };
    To copy to clipboard, switch view to plain text mode 

    with file.qml:
    javascript Code:
    1. import Qt 4.7
    2. import PushButton 1.0
    3.  
    4.  
    5. Rectangle {
    6. width: 300
    7. height: 300
    8. PushButtonItem {
    9. anchors.centerIn: parent
    10. }
    11. }
    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.


  7. #7
    Join Date
    Nov 2010
    Posts
    82
    Thanked 9 Times in 9 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: qwidget into QML

    i had a strange case mismatch warning that i didn't see

    anyway, thanks to you i understand it much better

    thank you

  8. #8
    Join Date
    Nov 2010
    Posts
    82
    Thanked 9 Times in 9 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: qwidget into QML

    I discovered that you can directly subclass QGraphicsProxyWidget and that works even better for resizing and positioning with QML

  9. #9
    Join Date
    Mar 2011
    Location
    united states
    Posts
    1
    Qt products
    PyQt3 PyQt4
    Platforms
    MacOS X

    Default Re: qwidget into QML

    the code of third snippet does not understand.

  10. #10

    Default Re: qwidget into QML

    Hi,

    I used the same way to try to embedded a QX11EmbedContainer into QML but the result is not really good :
    My goal is to use the QX11EmbedContainer to execute mplayer and to declare this one as a QML Element to be able to manipulate it into my *.qml files.
    The result is, when I execute it, the video is played on a new window and not on my QML window.

    Here is the code :

    Qt Code:
    1. /* videodisplayitem.h */
    2.  
    3. #include <QDeclarativeItem>
    4. #include <QX11EmbedContainer>
    5. #include <QtDeclarative>
    6. #include <QProcess>
    7. #include <QtGui>
    8.  
    9. class VideoDisplayItem : public QDeclarativeItem
    10. {
    11. Q_OBJECT
    12. public:
    13. VideoDisplayItem(QDeclarativeItem *parent =0) : QDeclarativeItem(parent) {
    14. container = new QX11EmbedContainer(NULL);
    15.  
    16. // if I execute container->show(); here, I get the video on a new window
    17. container->show();
    18.  
    19. QProcess * process = new QProcess(container);
    20. QString executable("/usr/bin/mplayer");
    21. QStringList arguments;
    22. arguments << "-wid";
    23. arguments << QString::number(container->winId());
    24. arguments << "/myvideo.mp4";
    25. process->start(executable, arguments);
    26.  
    27. proxy = new QGraphicsProxyWidget(this);
    28. proxy->setWidget(container);
    29. proxy->setPos(-container->sizeHint().width()/2, -container->sizeHint().height()/2);
    30.  
    31. // if I execute container->show(); here, I get nothing visible
    32. // container->show();
    33. }
    34. private:
    35. QX11EmbedContainer *container;
    36. QGraphicsProxyWidget *proxy;
    37.  
    38. };
    To copy to clipboard, switch view to plain text mode 


    Qt Code:
    1. /* main.cpp */
    2.  
    3. #include <QtGui/QApplication>
    4. #include "videodisplayitem.h"
    5.  
    6. int main(int argc, char *argv[])
    7. {
    8. QApplication app(argc, argv);
    9. QDeclarativeView view;
    10. qmlRegisterType<VideoDisplayItem>("VideoDisplay", 1, 0, "VideoDisplayItem");
    11. view.setSource(QUrl::fromLocalFile("file.qml"));
    12. view.show();
    13. return app.exec();
    14. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. /* file.qml */
    2.  
    3. import Qt 4.7
    4. import VideoDisplay 1.0
    5.  
    6. Rectangle {
    7. width: 300
    8. height: 300
    9.  
    10. VideoDisplayItem {
    11. anchors.centerIn: parent
    12. }
    13. }
    To copy to clipboard, switch view to plain text mode 

    If you get any idea or advise, thanks for all.
    My way to proceed might be totally wrong but I dont have any other ideas how to do.

    Thanks for all.

  11. #11
    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: qwidget into QML

    It won't work with Embed. Use appropriate API from QtMultimedia instead.
    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.


  12. #12

    Default Re: qwidget into QML

    Thanks for the answer.
    That is not a good news for me as I develop for embedded platform and I have to use a optimized version of MPlayer.
    Are there any other possibility using the Qt layouts ?

    There is an other thin I can't do for now, it's to display a transparent window over the QX11EmbedContainer, like an overlay..

    Thanks for all the advices.

  13. #13
    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: qwidget into QML

    The thing is that QML is implemented using Graphics View and items in the graphics view are not widgets so XEmbed won't work with them. You can use XEmbed with regular widgets. What exactly do you mean by "optimized" MPlayer? Because video decoding is done not by MPlayer but rather by codecs and Qt's multimedia tools will use the same codecs.
    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.


  14. #14
    Join Date
    Aug 2011
    Posts
    3
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows Symbian S60 Maemo/MeeGo

    Default Re: qwidget into QML

    I'm also trying to embed a QWidget into QML implementation (since there's no proper drawing primitives for QML like QWidget has).
    I tried the example wysota provided (the PushButton example above), but I stumbled upon a problem. It seems that resizing or setting the QWidget's size wont work.

    I tried:
    Qt Code:
    1. PushButtonItem {
    2. width: 300
    3. height: 300
    4. anchors.centerIn: parent
    5. }
    To copy to clipboard, switch view to plain text mode 

    And also:
    Qt Code:
    1. PushButtonItem {
    2. anchors.top: parent.top
    3. anchors.left: parent.left
    4. anchors.right: parent.right
    5. anchors.bottom: parent.bottom
    6. }
    To copy to clipboard, switch view to plain text mode 

    But with no luck

    So how to set the size of QDeclarativeItem that has a QWidget in it with QML?
    Or could this be a bug in the Symbian implementation of QDeclarativeItem?

  15. #15
    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: qwidget into QML

    Why would it work? Do you understand what my code does? Why would it resize the widget if you resize the declarative item? If you want to modify the widget in any way, you have to provide appropriate code that does that.
    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.


  16. #16
    Join Date
    Aug 2011
    Posts
    3
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows Symbian S60 Maemo/MeeGo

    Default Re: qwidget into QML

    So what do I need to implement? Is there some example that I could use to study how the resize events should be handled from "main QML" -> QDeclarativeItem -> QWidget?

    So what do I need to implement? Is there some example that I could use to study how the resize events should be handled from "main QML" -> QDeclarativeItem -> QWidget?


    Added after 1 47 minutes:


    I tied experimenting a bit.

    If I override the geometryChanged() default implementation with this, the resizing events propagate to the QWidget.

    Qt Code:
    1. void customDeclarativeItem::geometryChanged(const QRectF& newGeometry, const QRectF& oldGeometry)
    2. {
    3. mProxy->setGeometry(newGeometry);
    4. QDeclarativeItem::geometryChanged(newGeometry, oldGeometry);
    5. update();
    6. }
    To copy to clipboard, switch view to plain text mode 

    But the size of the widget seems to be wrong, so I'm not doing it right.
    Also the widget is not correctly drawn when needed. It worked OK before this, so there's something more I don't yet understand here.

    Can anyone help me with this?

    So the question is that how to get the QWidget inside a QGraphicsProxyWidget to automatically fill the QGraphicsProxyWidget completely?
    Last edited by Kung Foo; 26th August 2011 at 13:52.

  17. #17
    Join Date
    Aug 2011
    Posts
    3
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows Symbian S60 Maemo/MeeGo

    Default Re: qwidget into QML

    This is starting to sound like a monolog, but lets continue still..
    Here's how I got the QWidget-based component to show in correct size:

    Qt Code:
    1. void RadarItem::geometryChanged(const QRectF& newGeometry, const QRectF& oldGeometry)
    2. {
    3. if (newGeometry != oldGeometry) {
    4. // Solution #1:
    5. // mProxy->resize(newGeometry.width(), newGeometry.height());
    6. // Solution #2:
    7. // mProxy->setGeometry(QRectF(0.0, 0.0, newGeometry.width(), newGeometry.height()));
    8. }
    9. QDeclarativeItem::geometryChanged(newGeometry, oldGeometry);
    10. }
    To copy to clipboard, switch view to plain text mode 

    Both resize() and setGeometry() work, but there is now another problem.
    When I uncomment either of the solution lines above, the drawing of the widget ceases. The widget automatically redraws itself and it should show immediately on the UI. Now it doesnt, and the changes are only shown after I go to some other view and return back.

    How is this possible? How can resize() or setGeometry() cause this?

  18. #18
    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: qwidget into QML

    Maybe it sounds like a monolog, because I said everything I had to say in the subject. If someone uses some code he doesn't understand, there is nothing more to be said apart "understand the code before using it". If one understands the code and expands on it, he knows more than I do, because I didn't dig into the topic further. My plain simple answer is still the same -- "forward all the calls you want from the item to the widget". If you resize the item and you want the widget to resize as well then do exactly that -- resize the widget when the item is resized.
    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.


Similar Threads

  1. Replies: 1
    Last Post: 30th October 2010, 13:28
  2. Replies: 1
    Last Post: 16th September 2010, 16:57
  3. Replies: 1
    Last Post: 12th April 2010, 13:55
  4. Replies: 3
    Last Post: 2nd April 2010, 00:56
  5. Replies: 1
    Last Post: 2nd May 2006, 22: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.