July 22, 2011

xcoder xcoder
Lab Rat
80 posts

[SOLVED] Problems with changing text to QWidgets

 

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:

  1. #include <QtDeclarative/QDeclarativeExtensionPlugin>
  2. #include <QtDeclarative/qdeclarative.h>
  3. #include <QtGui/QGraphicsProxyWidget>
  4. #include <QtGui/QPushButton>
  5. #include <QDebug>
  6.  
  7.  
  8.  
  9. class MyPushButton : public QGraphicsProxyWidget
  10. {
  11.     Q_OBJECT
  12.     Q_PROPERTY(QString texts READ texts WRITE setsText)
  13.  
  14. public:
  15.     MyPushButton(QGraphicsItem* parent = 0);
  16.  
  17.     QString texts() const;
  18.     void setsText(const QString &texts;);
  19.  
  20.     QString name() const;
  21.     void setName(const QString &name;);
  22.  
  23.  
  24. private:
  25.     QPushButton *widget;
  26. };

mypushbutton.cpp:

  1. #include "mypushbutton.h"
  2.  #include <QtGui/QPushButton>
  3.  #include <QDebug>
  4.  
  5. MyPushButton::MyPushButton(QGraphicsItem* parent)
  6.     : QGraphicsProxyWidget(parent)
  7. {
  8.     widget = new QPushButton("MyPushButton");
  9.     widget->setAttribute(Qt::WA_NoSystemBackground);
  10.     setWidget(widget);
  11.  
  12.     //QObject::connect(widget, SIGNAL(clicked(bool)), this, SIGNAL(clicked(bool)));
  13. }
  14.  
  15.      QString MyPushButton::texts() const
  16.      {
  17.          return widget->text();
  18.      }
  19.  
  20.  
  21.      void MyPushButton::setsText(const QString &texts;)
  22.      {
  23.          if (texts != widget->text()) {
  24.              widget->setText(texts);
  25.              //emit textChanged();
  26.          }
  27.      }

main.cpp

  1. #include <QApplication>
  2. #include "piechart.h"
  3. #include "mypushbutton.h"
  4. #include <qdeclarative.h>
  5. #include <QDeclarativeView>
  6. #include "simple_app.h"
  7.  
  8. int main(int argc, char *argv[])
  9. {
  10.     QApplication app(argc, argv);
  11.  
  12.     qmlRegisterType<PieChart>("Charts", 1, 0, "PieChart");
  13.     qmlRegisterType<MyPushButton>("QWidget", 1, 0, "MyPushButton");
  14.  
  15.     simple_app *dialog = new simple_app;
  16.  
  17.     dialog->show();
  18.     return app.exec();
  19. }

and part of my qml

  1.   MyPushButton {
  2.              id: button1
  3.              x: 100; y: 100
  4.              texts: "Right"
  5.              transformOriginPoint: Qt.point(width / 2, height / 2)
  6.  
  7.          }

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

 Signature 

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

7 replies

July 22, 2011

mlong mlong
Robot Herder
1551 posts

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?

 Signature 

Senior Software Engineer
AccuWeather Enterprise Solutions
/* My views and opinions do not necessarily reflect those of my employer.  Void where prohibited. */

July 22, 2011

xcoder xcoder
Lab Rat
80 posts

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 [doc.qt.nokia.com]

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

 Signature 

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

July 22, 2011

mlong mlong
Robot Herder
1551 posts

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

main.cpp:

  1. #include <QtGui/QApplication>
  2. #include "qmlapplicationviewer.h"
  3. #include "mypushbutton.h"
  4. #include <qdeclarative.h>
  5. #include <QDeclarativeView>
  6.  
  7. int main(int argc, char *argv[])
  8. {
  9.    QApplication app(argc, argv);
  10.  
  11.    qmlRegisterType<MyPushButton>("QWidget", 1, 0, "MyPushButton");
  12.  
  13.    QmlApplicationViewer viewer;
  14.    viewer.setMainQmlFile(QLatin1String("qml/test/main.qml"));
  15.    viewer.showExpanded();
  16.  
  17.    return app.exec();
  18. }

mypushbutton.h:

  1. #ifndef MYPUSHBUTTON_H
  2. #define MYPUSHBUTTON_H
  3.  
  4. #include <QtDeclarative/QDeclarativeExtensionPlugin>
  5. #include <QtDeclarative/qdeclarative.h>
  6. #include <QtGui/QGraphicsProxyWidget>
  7. #include <QtGui/QPushButton>
  8. #include <QDebug>
  9.  
  10. class MyPushButton : public QGraphicsProxyWidget
  11. {
  12.     Q_OBJECT
  13.     Q_PROPERTY(QString texts READ texts WRITE setsText)
  14.  
  15. public:
  16.     MyPushButton(QGraphicsItem* parent = 0);
  17.  
  18.     QString texts() const;
  19.     void setsText(const QString &texts);
  20.  
  21. private:
  22.     QPushButton *widget;
  23. };
  24.  
  25. #endif // MYPUSHBUTTON_H

mypushbutton.cpp:

  1. #include "mypushbutton.h"
  2.  
  3. MyPushButton::MyPushButton(QGraphicsItem *parent) :
  4.     QGraphicsProxyWidget(parent)
  5. {
  6.    widget = new QPushButton("MyPushButton");
  7.    widget->setAttribute(Qt::WA_NoSystemBackground);
  8.    setWidget(widget);
  9.  
  10. }
  11.  
  12. QString MyPushButton::texts() const
  13.    {
  14.        return widget->text();
  15.    }
  16.  
  17.    void MyPushButton::setsText(const QString &texts)
  18.    {
  19.        if (texts != widget->text()) {
  20.            widget->setText(texts);
  21.        }
  22.    }

main.qml:

  1. import QtQuick 1.0
  2. import QWidget 1.0
  3.  
  4. Rectangle {
  5.    width: 360
  6.    height: 360
  7.  
  8.    MyPushButton {
  9.       id: button1
  10.       x: 100; y: 100
  11.       texts: "Right"
  12.       transformOriginPoint: Qt.point(width / 2, height / 2)
  13.    }
  14. }

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.

 Signature 

Senior Software Engineer
AccuWeather Enterprise Solutions
/* My views and opinions do not necessarily reflect those of my employer.  Void where prohibited. */

July 22, 2011

xcoder xcoder
Lab Rat
80 posts

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

 Signature 

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

July 22, 2011

mlong mlong
Robot Herder
1551 posts

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

 Signature 

Senior Software Engineer
AccuWeather Enterprise Solutions
/* My views and opinions do not necessarily reflect those of my employer.  Void where prohibited. */

July 22, 2011

xcoder xcoder
Lab Rat
80 posts

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!

 Signature 

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

July 22, 2011

mlong mlong
Robot Herder
1551 posts

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.

 Signature 

Senior Software Engineer
AccuWeather Enterprise Solutions
/* My views and opinions do not necessarily reflect those of my employer.  Void where prohibited. */

 
  ‹‹ Row and Column align?      Global js to use in more QML files (SOLVED) ››

You must log in to post a reply. Not a member yet? Register here!