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. [SOLVED] Problems with changing text to QWidgets
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] Problems with changing text to QWidgets

Scheduled Pinned Locked Moved QML and Qt Quick
8 Posts 2 Posters 3.4k Views 1 Watching
  • 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.
  • X Offline
    X Offline
    xcoder
    wrote on last edited by
    #1

    I can't figure out why I can't change the text for my button!

    When I try to run my app it just returns: "file:///home/user/Desktop/Bizness/simple_gui/myqml.qml:30:14: Cannot assign to non-existent property "texts"
    texts: "Right"
    ^ "

    Here is my mypushbutton.h:
    @#include <QtDeclarative/QDeclarativeExtensionPlugin>
    #include <QtDeclarative/qdeclarative.h>
    #include <QtGui/QGraphicsProxyWidget>
    #include <QtGui/QPushButton>
    #include <QDebug>

    class MyPushButton : public QGraphicsProxyWidget
    {
    Q_OBJECT
    Q_PROPERTY(QString texts READ texts WRITE setsText)

    public:
    MyPushButton(QGraphicsItem* parent = 0);

    QString texts() const;
    void setsText(const QString &texts;);
    
    QString name() const;
    void setName(const QString &name;);
    

    private:
    QPushButton *widget;
    };
    @

    mypushbutton.cpp:
    @#include "mypushbutton.h"
    #include <QtGui/QPushButton>
    #include <QDebug>

    MyPushButton::MyPushButton(QGraphicsItem* parent)
    : QGraphicsProxyWidget(parent)
    {
    widget = new QPushButton("MyPushButton");
    widget->setAttribute(Qt::WA_NoSystemBackground);
    setWidget(widget);

    //QObject::connect(widget, SIGNAL(clicked(bool)), this, SIGNAL(clicked(bool)));
    

    }

     QString MyPushButton::texts() const
     {
         return widget->text();
     }
    
    
     void MyPushButton::setsText(const QString &texts;)
     {
         if (texts != widget->text()) {
             widget->setText(texts);
             //emit textChanged();
         }
     }
    

    @

    main.cpp
    @#include <QApplication>
    #include "piechart.h"
    #include "mypushbutton.h"
    #include <qdeclarative.h>
    #include <QDeclarativeView>
    #include "simple_app.h"

    int main(int argc, char *argv[])
    {
    QApplication app(argc, argv);

    qmlRegisterType<PieChart>("Charts", 1, 0, "PieChart");
    qmlRegisterType<MyPushButton>("QWidget", 1, 0, "MyPushButton");
    
    simple_app *dialog = new simple_app;
    
    dialog->show();
    return app.exec&#40;&#41;;
    

    }
    @

    and part of my qml
    @ MyPushButton {
    id: button1
    x: 100; y: 100
    texts: "Right"
    transformOriginPoint: Qt.point(width / 2, height / 2)

         }
    

    @

    However, if I delete this line "texts: "right"" my app runs fine and shows the QWidget button!

    Only a biker knows why a dog sticks his head out of a car window.

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mlong
      wrote on last edited by
      #2

      The code you posted works for me (Assuming an import QWidget 1.0 at the head of the QML file.) What platform/version are you using?

      Software Engineer
      My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

      1 Reply Last reply
      0
      • X Offline
        X Offline
        xcoder
        wrote on last edited by
        #3

        xubuntu 11.04 32bit, Qt Creator 2.2.1

        And yes I've an "import QWidget 1.0" at the top of my QML file

        Funny that I've successfully implanted "this example":http://doc.qt.nokia.com/4.7-snapshot/declarative-tutorials-extending-chapter2-methods.html

        But I can't get to work with simple button.

        Maybe I'll just install opensuse, because lately the new version of ubuntu has been giving my some headaches. I tried LTS, but I need newer apps than LTS has. And the new 11.04 version has a lot of glitches!

        Best Regards
        Raivis

        Only a biker knows why a dog sticks his head out of a car window.

        1 Reply Last reply
        0
        • M Offline
          M Offline
          mlong
          wrote on last edited by
          #4

          Here's the code I ended up trying (starting with a Creator-generated QML project.)

          main.cpp:
          @
          #include <QtGui/QApplication>
          #include "qmlapplicationviewer.h"
          #include "mypushbutton.h"
          #include <qdeclarative.h>
          #include <QDeclarativeView>

          int main(int argc, char *argv[])
          {
          QApplication app(argc, argv);

          qmlRegisterType<MyPushButton>("QWidget", 1, 0, "MyPushButton");

          QmlApplicationViewer viewer;
          viewer.setMainQmlFile(QLatin1String("qml/test/main.qml"));
          viewer.showExpanded();

          return app.exec();
          }
          @

          mypushbutton.h:
          @
          #ifndef MYPUSHBUTTON_H
          #define MYPUSHBUTTON_H

          #include <QtDeclarative/QDeclarativeExtensionPlugin>
          #include <QtDeclarative/qdeclarative.h>
          #include <QtGui/QGraphicsProxyWidget>
          #include <QtGui/QPushButton>
          #include <QDebug>

          class MyPushButton : public QGraphicsProxyWidget
          {
          Q_OBJECT
          Q_PROPERTY(QString texts READ texts WRITE setsText)

          public:
          MyPushButton(QGraphicsItem* parent = 0);

          QString texts() const;
          void setsText(const QString &amp;texts);
          

          private:
          QPushButton *widget;
          };

          #endif // MYPUSHBUTTON_H
          @

          mypushbutton.cpp:
          @
          #include "mypushbutton.h"

          MyPushButton::MyPushButton(QGraphicsItem *parent) :
          QGraphicsProxyWidget(parent)
          {
          widget = new QPushButton("MyPushButton");
          widget->setAttribute(Qt::WA_NoSystemBackground);
          setWidget(widget);

          }

          QString MyPushButton::texts() const
          {
          return widget->text();
          }

          void MyPushButton::setsText(const QString &texts)
          {
          if (texts != widget->text()) {
          widget->setText(texts);
          }
          }
          @

          main.qml:
          @
          import QtQuick 1.0
          import QWidget 1.0

          Rectangle {
          width: 360
          height: 360

          MyPushButton {
          id: button1
          x: 100; y: 100
          texts: "Right"
          transformOriginPoint: Qt.point(width / 2, height / 2)
          }
          }
          @

          You might give it a shot and see if that works for you. If so, it might give you a better starting point for debugging.

          Software Engineer
          My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

          1 Reply Last reply
          0
          • X Offline
            X Offline
            xcoder
            wrote on last edited by
            #5

            Thanks your given code works, Now I'm going to try and figure out whats wrong with main!

            Only a biker knows why a dog sticks his head out of a car window.

            1 Reply Last reply
            0
            • M Offline
              M Offline
              mlong
              wrote on last edited by
              #6

              Great! When you figure it out, let us know!

              Software Engineer
              My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

              1 Reply Last reply
              0
              • X Offline
                X Offline
                xcoder
                wrote on last edited by
                #7

                Found the problem! A glitch in Qt Creator.
                Both of the codes are practically identical!

                I just changed the name of function, and suddenly my compiler returns this kind of error:
                /home/user/Desktop/Bizness/simple_gui/mypushbutton.cpp:-1: error: undefined reference to `vtable for

                Actually 11 of them, all "undefined refernece to 'vtable ......"

                I just deleted all *.o files in my application directory. Rebuilt my project, and everything is OK. That is why the code you gave me worked for me, because I created new project, and so it generated new *.o files!

                Thanks!

                Only a biker knows why a dog sticks his head out of a car window.

                1 Reply Last reply
                0
                • M Offline
                  M Offline
                  mlong
                  wrote on last edited by
                  #8

                  Probably rerunning qmake would have fixed things up. Glad to know it works now!

                  Be sure to add [Solved] to the front of the thread title.

                  Software Engineer
                  My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

                  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