[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:
    @#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!


  • Moderators

    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?



  • 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


  • Moderators

    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.



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


  • Moderators

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



  • 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!


  • Moderators

    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.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.