Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. component loaded but is not shown in window

component loaded but is not shown in window

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
2 Posts 2 Posters 246 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Mighty_Pig
    wrote on last edited by
    #1

    So I created my custom component and loaded it as a plugin. It is also constructed, since I get a qInfo() from the constructor. However, it does not show on the window.

    As stated in the documentation, you should override the paint() function, which I did. However, it seems this is never called. When should this be called, and what could a reason be that it never is called?

    My code looks as following:
    mycustombuttonnew.qml(The plugin):

    import QtQuick 6.6
    import QtQuick.Controls
    import QtQuick.Controls.Material
    
    Item {
        id: button
        Material.background: Material.Red
        width: button.getWidth()
        height: button.getHeight()
        objectName: "MyCustomButton"
        Text {
                    id: name
                    text: button.name // Bind to the parent's text property
                    color: "black"
                    anchors.centerIn: button // Center text within the button
                    objectName: "MyCustomButtonText"
                }
        MouseArea {
                    id: mouseArea
                    anchors.fill: button
                    onClicked: {
                        button.buttonClicked()
                    }
                    objectName: "MyCustomButtonMouseArea"
                }
    }
    

    Then I include this custom component/plugin inside the mainwindow.qml:

    import QtQuick 6.6
    
    import plugins.MyCustomButtonNew
    import QtQuick.Controls.Material
    
    Window {
        visible: true
        Item {
            width: 1920; height: 1080
            objectName: "root item"
    
            MyCustomButtonNew {
                id: button
                Material.background: Material.Red
                name: "test"
                objectName: "MyCustomButton"
    
            }
        }
    }
    

    My main is as following:

    int main(int argc, char *argv[])
    {
    
        QApplication a(argc, argv);
        QQmlApplicationEngine engine;
        QWindow window;
        const QUrl url = QUrl::fromLocalFile("mainwindow.qml");
        QObject::connect(
            &engine,
            &QQmlApplicationEngine::objectCreated,
            &a,
            [url](QObject *obj, const QUrl &objUrl) {
                if (!obj && url == objUrl)
                    QCoreApplication::exit(-1);
            },
            Qt::QueuedConnection);
        engine.load(url);
        QQuickView view = QQuickView(&engine, &window);
     view.setResizeMode(QQuickView::SizeRootObjectToView);
        view.show();
    return a.exec();
    

    Any help to make me understand this better would be appriciated :).

    GrecKoG 1 Reply Last reply
    0
    • M Mighty_Pig

      So I created my custom component and loaded it as a plugin. It is also constructed, since I get a qInfo() from the constructor. However, it does not show on the window.

      As stated in the documentation, you should override the paint() function, which I did. However, it seems this is never called. When should this be called, and what could a reason be that it never is called?

      My code looks as following:
      mycustombuttonnew.qml(The plugin):

      import QtQuick 6.6
      import QtQuick.Controls
      import QtQuick.Controls.Material
      
      Item {
          id: button
          Material.background: Material.Red
          width: button.getWidth()
          height: button.getHeight()
          objectName: "MyCustomButton"
          Text {
                      id: name
                      text: button.name // Bind to the parent's text property
                      color: "black"
                      anchors.centerIn: button // Center text within the button
                      objectName: "MyCustomButtonText"
                  }
          MouseArea {
                      id: mouseArea
                      anchors.fill: button
                      onClicked: {
                          button.buttonClicked()
                      }
                      objectName: "MyCustomButtonMouseArea"
                  }
      }
      

      Then I include this custom component/plugin inside the mainwindow.qml:

      import QtQuick 6.6
      
      import plugins.MyCustomButtonNew
      import QtQuick.Controls.Material
      
      Window {
          visible: true
          Item {
              width: 1920; height: 1080
              objectName: "root item"
      
              MyCustomButtonNew {
                  id: button
                  Material.background: Material.Red
                  name: "test"
                  objectName: "MyCustomButton"
      
              }
          }
      }
      

      My main is as following:

      int main(int argc, char *argv[])
      {
      
          QApplication a(argc, argv);
          QQmlApplicationEngine engine;
          QWindow window;
          const QUrl url = QUrl::fromLocalFile("mainwindow.qml");
          QObject::connect(
              &engine,
              &QQmlApplicationEngine::objectCreated,
              &a,
              [url](QObject *obj, const QUrl &objUrl) {
                  if (!obj && url == objUrl)
                      QCoreApplication::exit(-1);
              },
              Qt::QueuedConnection);
          engine.load(url);
          QQuickView view = QQuickView(&engine, &window);
       view.setResizeMode(QQuickView::SizeRootObjectToView);
          view.show();
      return a.exec();
      

      Any help to make me understand this better would be appriciated :).

      GrecKoG Offline
      GrecKoG Offline
      GrecKo
      Qt Champions 2018
      wrote on last edited by
      #2

      @Mighty_Pig What if you set a width and height on the Window?

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved