Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

A few design questions...



  • Decisions, decisions. An absolute pathname, and I bet it won't port to other platforms. Relative pathnames seem weird here, too.

    I don't suppose Qt supplies a pathname for the project, does it? I can't see how it could, but that would solve the problem.



  • What you can do is ionstall the file besides you exe and use QCoreApplication::applicationDirPath()



  • What you can do is ionstall the file besides you exe and use QCoreApplication::applicationDirPath()

    True, but then the QML file is out of the source structure. I'm trying to preserve a separation between source files (of all kinds) and makefile output. It seems like good CM practice if nothing else. I guess I'm probably stuck with the relative pathnames, huh?



  • Typically, you design the app so it will work on target machines with installed files, not with developer structures. Make it part of the makefile to copy the file to the out dir?



  • Now THAT'S a good idea, and will obviate the issue completely. But...makefiles are automatically generated, right? How can I do this in such a way that will survive blowing away the makefile and regenerating it?



  • read the qmake docs. I'm sure there is some possibility, but I never needed that :-)



  • But...it's not really a qmake function, is it? Because this special behavior should survive the makefile. Is there a way to do this in the project file?





  • You can do this by defining a custom target in your .pro file. Something like this will do it:

    @
    myresources.path = $$(MY_INSTALL_ROOT)/resources
    myresources.files = myqmlfile.qml

    INSTALLS += target myresources
    @

    The above depends upon the environment variable $MY_INSTALL_ROOT being set. However, this hardcodes the install path.

    Instead, it is better to get rid of the $$(MY_INSTALL_ROOT) part of the path and use the DESTDIR feature of make to specify your installation root:

    @make DESTDIR=/my/installation/path install@

    That separates the install path from your project files.



  • Sorry if I seem a little slow on the uptake here -- I'm coping with multiple unfamiliar concepts. It's been over 20 years since I've used makefiles, and I'm still trying to figure out project files, qmake, QDir and other stuff.

    Zap: I like the way your approach looks. I can't find anything on DESTDIR in the docs, though. It's not clear to me how this approach obviates the hardcoding issue; it seems to just postpone it. Obviously I'm not understanding the whole thing.

    Thanks.



  • First: Go with the Resource directory in the application bundle. That's the expected path on OS X. One should not change those things only when it is absolutely necessary.

    qmake has support for adding additional files to the bundle:

    @
    APP_QML_FILES.files = path/to/file1.qml path/to/file2.qml
    APP_QML_FILES.path = Contents/Resources
    QMAKE_BUNDLE_DATA += APP_QML_FILES
    @

    This installes the files to YourFancyApplication.app/Contents/Resources - just where it belongs on OS X :)

    EDIT: Some more background links in the new "wiki article":http://developer.qt.nokia.com/wiki/Resource_files_in_OS_X_bundle



  • Thanks, Volker. I realize that files should be in the application bundle, and I wasn't suggesting to do it a different way. In the back of my mind, though, I'm wondering what that bundle looks like when copied to a PC. I'm hoping it's a directory.

    Do I correctly assume the path/to refers to the path to the source code? Or is that the target?

    EDIT:

    I tried it two ways:
    @APP_QML_FILES.files = ./DemodShaperFilter.qml
    APP_QML_FILES.path = Contents/Resources
    QMAKE_BUNDLE_DATA += APP_QML_FILES
    @

    and:
    @APP_QML_FILES.files = DemodShaperFilter.qml
    APP_QML_FILES.path = Contents/Resources
    QMAKE_BUNDLE_DATA += APP_QML_FILES
    @

    Neither put the file into the Resources folder. It appears that the path is required, which surprises me, because I figured that the .pro file would use its own directory as the default.

    So now, the question is: is there some slick way to derive the current path from an environment variable or something, and plug it into the .pro file automatically?

    I'm not pursuing this level of automation out of laziness; it's more a matter of having one less thing to remember when stuff gets changed.

    Thanks.



  • [quote author="mzimmers" date="1301602502"]Thanks, Volker. I realize that files should be in the application bundle, and I wasn't suggesting to do it a different way. In the back of my mind, though, I'm wondering what that bundle looks like when copied to a PC. I'm hoping it's a directory.

    Do I correctly assume the path/to refers to the path to the source code? Or is that the target?[/quote]

    Thats the path of the source. The target path (relative to the bundle) is in the second line.

    If you just care about copying the Mac bundle to a windows box, then you're right, it is nothing more than an ordinary directory. On the Mac command line you just "cd" into it, like into every other directory. It's just the Finder that displays the bundle differently and if you call "open myprogram.app" on the command line, it does not open Finder (like for directories) but launches the application.

    Regarding deployment on Windows, that's a different beast. You'll probably consider using an installer to put everything in place. You find some hints on windows installers in "this thread":http://developer.qt.nokia.com/forums/viewthread/4498



  • Volker: you posted about the same time I edited my last post, so I'm not sure if you saw my new comments/questions?



  • The path is relative to the location of your .pro file.



  • Hmm...well, then it should have worked, no?

    What totally obvious thing am I overlooking? I could post the entire .pro file if that would help.



  • Yep, it should have worked. Is the QML file in a subdirectory?

    You can try with a test file like this:

    @
    APP_QML_FILES.files = Makefile
    APP_QML_FILES.path = Contents/Resources
    QMAKE_BUNDLE_DATA += APP_QML_FILES
    @

    Not that a Makefile in the Resources is very useful, but it serves well for demonstration purposes :-)

    For another test, you could add the absolute path to the QML to APP_QML_FILES.files.

    And as a third guess, do you use shadow builds?



  • QML file is in same directory as .pro file.

    Regarding adding the absolute path: I'll re-ask my question of whether there's a way to do it symbolically. Otherwise, it doesn't really solve the problem.

    Shadow builds: I have a parallel directory to my source, called NNN-build-desktop. That's where all my built files go to live. Is that a shadow build, and if so, is it a bad thing, and if so, how do I do something else?

    Thanks.



  • I don't know about a function that returns the path of the .pro file. As far as I remember this question raised in the forums some time ago.

    Shadow builds are not a problem. I just checked with my test project. (EDIT: yep, the parallel directory is a shadow build)

    It must be something different, probably a typo or so. You can post the .pro file here and the contents of your directory, so we can check again. If you don't want to expose your data publicly, contact me via private message to negotiate a "secure" channel.



  • No, I think it's safe to share filenames, for now at least.

    Here's the .pro file:

    @######################################################################

    Automatically generated by qmake (2.01a) Mon Mar 28 17:09:16 2011

    ######################################################################

    TEMPLATE = app
    TARGET =
    DEPENDPATH += . headers src
    INCLUDEPATH += . headers

    APP_QML_FILES.files = DemodShaperFilter.qml
    APP_QML_FILES.path = Contents/Resources
    QMAKE_BUNDLE_DATA += APP_QML_FILES

    QT += declarative

    Input

    HEADERS +=
    headers/clock.h
    headers/DemodShaperFilter.h
    headers/globals.h
    headers/register_offsets.h
    headers/Soc.h
    headers/SocReg.h
    headers/widget.h
    headers/GenericCell.h
    SOURCES +=
    src/clock.cpp
    src/DemodShaperFilter.cpp
    src/filestuff.cpp
    src/globals.cpp
    src/main.cpp
    src/Soc.cpp
    src/SocReg.cpp
    src/widget.cpp
    src/GenericCell.cpp

    FORMS +=
    widget.ui

    OTHER_FILES +=
    DemodShaperFilter.qml
    @

    And here's a snapshot of my directory:

    !http://www.scopedin.com/images/screen.jpg(directory)!

    Thanks...



  • I found the problem: Contents/Resources isn't where the executable is; it's in Contents/MacOS. Works fine now.

    That was a nice diversion; now I'll get back to trying to do what Zap mentioned several posts ago. I'll be back with questions about that soon.



  • So, Zap: from reading the section on Q_PROPERTY, it appears that within my top-level class, I replace the variable creation with a Q_PROPERTY. So, instead of:

    @int combGainI;@

    I'll now have something like:

    @ Q_PROPERTY (int combGainI READ readFn WRITE writeFn);
    @

    Correct so far?

    So, since I have to supply a read and write function, am I correct in assuming that this can no longer be an int? I have to make a class for the combGain, and provide gets and sets for it?

    Thanks.



  • You need the Q_PROPERTY declaration as well as not instead of your normal member variable declaration. This aspect yoru class might look something like this:

    @
    class MyClass : public QObject
    {
    Q_OBJECT
    Q_PROPERTY( int combGainl READ combGainl WRITE setCombGainl NOTIFY combGainlChanged )

    public:
    ...
    void setCombGainl( int combGainl )
    {
    // Only emit signal if new value is different
    if ( m_combGainl == combGainl )
    return;
    m_combGainl = combGainl;
    emit combGainlChanged( m_combGainl );
    }

    int combGainl() const { return m_combGainl; }
    

    signals:
    void combGainlChanged( int );

    private:
    int m_combGainl;
    };
    @

    The important aspects here are:

    You still need to decalre the member as usual

    You need to provide a getter method

    If you declare the property as writeable (from outside the class) as I have illustrated here, then you must provide a setter function too. It is important (to minimise updates and to break circular dependencies between bound properties) that the setter function only emits the property changed notification signal when it does actually change.

    If the variable exposed as a property is purely calculated internally then you do not need to expose it as a writeable property. However, you do need to emit the combGainlChanged( int ) signal when you calculate a new value for it. This is used by the QML framework to update QML items that have properties bound to this property.

    I hope all of that makes sense. It will become clear when you get your first C++/QML example working.

    As for the property types, you can use any type known to Qt's metatype system (look up Q_DECLARE_METATYPE and qRegisterMetaType in the docs). So all the built in types (int, double, etc. plus all the types handled by QVariant work fine out of the box).



  • [quote author="mzimmers" date="1301619372"]I found the problem: Contents/Resources isn't where the executable is; it's in Contents/MacOS. Works fine now.

    That was a nice diversion; now I'll get back to trying to do what Zap mentioned several posts ago. I'll be back with questions about that soon.[/quote]

    But it got copied into Contents/Resources?

    A good way would be to put it there and adapt the search paths in your application (wrapped in some #ifdef):

    @
    #if defined( Q_OS_MACX )
    declarativeEngine.addImportPath(qApp->applicationDirPath() + "/../Resources");
    #endif
    @

    (I've not tested it, you might use another path setter/adder for this)



  • Zap: that's a very good explanation. I'm still unclear on the combGainIChanged function, though. Do I actually implement this, or is it part of the BFM of Qt? I ask because I don't see an analog for it in the code we did in the other thread.

    It seems that I'm not yet tying data to a display item, too...is that the next step?

    EDIT:

    Another question: can the combGainI stuff be adapted to be general-purpose, or do I need to replicate this for each variable I wish to display with QML?



  • Hey, Volker:

    Just to be sure I was clear: when qmake runs, it puts the executable into Contents/MacOS, not into Contents/Resources. So, I just modified my .pro file accordingly:

    @APP_QML_FILES.files = DemodShaperFilter.qml
    APP_QML_FILES.path = Contents/MacOS
    QMAKE_BUNDLE_DATA += APP_QML_FILES
    @

    Unless I'm missing something, I don't see a downside to putting the .qml file (and other needed files) into the same directory as the executable...



  • In theory, Contents/MacOS conatains the executable(s), and Contents/Resources would be the place to put QML files. It's not really harmful, to put resources like QMLs into Contents/MacOS too. The only drawback is that it is not best practise on OS X and that Apple recommends Contents/Resources.

    What makes me wonder, is how qmake puts that files into the wrong dir. Assuming there are two files QuickTest.qml and TestPage.qml within the same directory as the .pro file, the following snippet puts it into Contents/Resources:

    @
    APP_QML_FILES.files = QuickTest.qml TestPage.qml
    APP_QML_FILES.path = Contents/Resources
    QMAKE_BUNDLE_DATA += APP_QML_FILES
    @

    You might want to remove the application bundle directory before building, as qmake just adds files, but does not remove them!



  • [quote author="mzimmers" date="1301678701"]Zap: that's a very good explanation. I'm still unclear on the combGainIChanged function, though. Do I actually implement this, or is it part of the BFM of Qt? I ask because I don't see an analog for it in the code we did in the other thread.
    [/quote]

    No you do not need to implement it. The implementation of signals is generated automatically for you by the moc step of the build process. When you run qmake, it scans the files listed in the HEADERS section for class containing Q_OBJECT. It adds rules to the generated Makefile to run these headers through the Qt moc tool. The moc generates the moc_*.cpp files that you will find in your build tree. The signal/slot and other metaobject magic is implemented in these files.

    [quote author="mzimmers" date="1301678701"]
    It seems that I'm not yet tying data to a display item, too...is that the next step?
    [/quote]

    Yes we will tackle that next. A swim and some food first for me though ;-)

    EDIT:

    [quote author="mzimmers" date="1301678701"]
    Another question: can the combGainI stuff be adapted to be general-purpose, or do I need to replicate this for each variable I wish to display with QML?[/quote]

    That depends on what you want to do. You need to expose all properties that you wish to access from QML in this way. If you have a lot of data then there are alternative approaches using a subclass of QAbstractListModel for example.

    Which approach to use depends upon how you structure your code. From what you have said earlier it sounded like you will have an object hierarchy, so it may be simplest to expose a member or two from each object as properties in this way. If you have a class with 20 such data items then it may pay you to look into the model approach for that case.

    It will become clearer as we progress...



  • OK...I've implemented the stuff in the post above. Is there any way to verify it at this point, or do we just go to the next step?



  • You can verify what you have done so far by converting your original GUI to use the new property changed signals instead of the original dataChanged( int, int ) signal. So if you now have two properties, one for each of the two variables, then you will need to make two connections - one for each property notifier signal.

    Another way of verifying that it works would be to make use of the QTestLib framework and write a unit test for your class. In such tests you can make use of QSignalSpy to ensure that the signals were emitted as expected. Unit tests are nice as they can be run at any time in the future if you need to convince yourself that a certain part of your codebase is still working as it should.

    Once you have verified that you calculation works as it should you can start putting together your QML scene that will display the state of your calculation object's properties. See if you can have a go at that from what you find in the QML examples. At this stage we don't need anything extravagant, just a couple of rectangles and text elements should suffice. Let us know if you need a hand with this.



  • Just got back to this stuff. So, if I understand your first paragraph above, I want to change this line in widget.cpp:

    @ connect (soc, SIGNAL(dataChanged(int, int)), this, SLOT(setValue(int, int)));@

    to:

    @ connect (soc, SIGNAL(combGainIChanged(int)), this, SLOT(setCombGainI(int)));@

    Is this right? If so, what should I expect to see happen?

    EDIT:

    Actually, instead of changing that line, can I just add the second line (if correct)? For now, I might as well keep the original GUI working, too.
    Thanks.



  • Yes replace the original line with the second one that you posted. Then you need to implement the slot setCombGainI(int) such that it updates that value in your GUI.

    You would then need a corresponding slot for any other variable, combGainQ say.

    If you keep the original connections how can you know that your new signals are being emitted when they should be and with the right values?

    I would seriously consider adding a simple unit test. Have a read of the QTest Framework docs. They have a nice "tutorial":http://doc.qt.nokia.com/latest/qtestlib-tutorial.html that explains it all. Trust me it can be very surprising what unit tests can show up in the course of testing. As long as you writie good tests they are very valuable things to have around.



  • [quote author="ZapB" date="1301904635"]Yes replace the original line with the second one that you posted. Then you need to implement the slot setCombGainI(int) such that it updates that value in your GUI.[/quote]

    Right, and this is the missing ingredient, isn't it? Currently, setCombGainI() emits this:

    @ emit combGainIChanged (combGainI);@

    So, what do I do now to "tie together" the code and the GUI?

    [quote]You would then need a corresponding slot for any other variable, combGainQ say.[/quote]

    Yeah, I figured I'd get one right and then do the other.

    [quote]If you keep the original connections how can you know that your new signals are being emitted when they should be and with the right values? [/quote]

    I guess I'm missing something. I thought if I maintained the two GUIs, I could verify the new one by comparing its results with the original. Plus, I still have the console output. Or, is this not what you're asking?

    [quote]I would seriously consider adding a simple unit test. Have a read of the QTest Framework docs. They have a nice "tutorial":http://doc.qt.nokia.com/latest/qtestlib-tutorial.html that explains it all. Trust me it can be very surprising what unit tests can show up in the course of testing. As long as you writie good tests they are very valuable things to have around.[/quote]

    I'll do this, I promise. I just thought it would be better for me to have a little better grasp of what I'm doing here before I went about designing a test for it.

    Thanks.



  • [quote author="mzimmers" date="1301916754"][quote author="ZapB" date="1301904635"]Yes replace the original line with the second one that you posted. Then you need to implement the slot setCombGainI(int) such that it updates that value in your GUI.[/quote]

    Right, and this is the missing ingredient, isn't it? Currently, setCombGainI() emits this:

    @ emit combGainIChanged (combGainI);@
    [/quote]

    I meant that you need to implement a setCombGainI( int ) slot on your GUI class (Widget in the original example). This should modify the text string passed to the QLabel.

    As far as being able to maintain your old working GUI I suggest that you do the new work in a branch of your version control system. Then you can go back and forth between the old method and the new one at will.

    BTW having a unit test for the calculation part (the class with the properties) does not mean you abandon the existing GUI code. The unit test becomes an executable that exists parallel to your GUI binary. It's just that it tests one aspect (usually one class) of your code-base.



  • I meant that you need to implement a setCombGainI( int ) slot on your GUI class (Widget in the original example). This should modify the text string passed to the QLabel.

    OK, so I'm going to modify setValue, right? Apart from (temporarily) changing it to work with only one variable, what else am I going to do? It still seems like I'm missing the "hook up" to a "real" GUI. Am I still going to use the ui->label->setText call for this?



  • Yes that is right. The bit that I think is still missing is modifying the class with the properties to emit the notification signals when you have calculated new values.

    Modifying the setData() slot to work with a single int and modifying the connect call should be all that is needed in addition.

    I'll try to get a simple QML demo up and running shortly that shows how this will fit together.



  • OK, thanks, Zap. I'll stand by to hear from you on this before I make these changes. So, will I be doing some work within Designer for this upcoming step?

    EDIT: hey, Zap...can you give me a sneak preview as to what the call to the new GUI will look like (or at least where to find it in the docs?) That might be enough to get me launched on this.

    Thanks.



  • Righto here goes...I have had to split this into multiple posts as the original was too large for a single post. Maybe I should turn this in to a wiki article ;-)

    I have chosen to modify the original example that I gave you in the other thread - the one with our custom Widget class and the Generator class. I have renamed the property to combGainI to match your naming scheme so that it is easier for you to see how it would map across to your case.

    In this example I have decided to keep the old QWidget based GUI and to extend it with a QML based widget that operates in parallel. This way you can see how both types of GUI can use the same C++ backend as a data source. I could of course added in a third view which was based on QGraphicsView but that is probably too much for a simple example ;-)

    I'll present it as if we are starting from scratch but more briefly than before. So let's get started...

    In qt-creator create a new Qt C++ project and choose QWidget as the base class of our main widget. Use designer to edit the ui file and add a QLabel and a QDeclarative view and lay them out vertically in the widget as shown below.

    !http://www.theharmers.co.uk/simple-example-form.png(designer-form)!

    The two parts of the form demonstrate the use of QWidget-based GUI elements and using QML for a declarative approach. For completeness the raw xml for widget.ui is:

    @
    <?xml version="1.0" encoding="UTF-8"?>
    <ui version="4.0">
    <class>Widget</class>
    <widget class="QWidget" name="Widget">
    <property name="geometry">
    <rect>
    <x>0</x>
    <y>0</y>
    <width>494</width>
    <height>328</height>
    </rect>
    </property>
    <property name="windowTitle">
    <string>Widget</string>
    </property>
    <layout class="QVBoxLayout" name="verticalLayout">
    <item>
    <widget class="QLabel" name="label">
    <property name="text">
    <string>CombGainI =</string>
    </property>
    <property name="alignment">
    <set>Qt::AlignCenter</set>
    </property>
    </widget>
    </item>
    <item>
    <widget class="QDeclarativeView" name="declarativeView">
    <property name="sizePolicy">
    <sizepolicy hsizetype="Preferred" vsizetype="Expanding">
    <horstretch>0</horstretch>
    <verstretch>0</verstretch>
    </sizepolicy>
    </property>
    </widget>
    </item>
    </layout>
    </widget>
    <layoutdefault spacing="6" margin="11"/>
    <customwidgets>
    <customwidget>
    <class>QDeclarativeView</class>
    <extends>QGraphicsView</extends>
    <header>QtDeclarative/QDeclarativeView</header>
    </customwidget>
    </customwidgets>
    <resources/>
    <connections/>
    </ui>
    @

    The main() function just creates our custom Widget and shows it:

    @
    #include <QtGui/QApplication>
    #include "widget.h"

    int main( int argc, char* argv[] )
    {
    QApplication a( argc, argv );
    Widget w;
    w.show();
    return a.exec();
    }
    @



  • Now we get to the interesting parts. We'll take a look at the Generator class first. The generator class is what is used to calculate new values of a property. Here is the class declaration:

    @
    #ifndef GENERATOR_H
    #define GENERATOR_H

    #include <QObject>

    class Generator : public QObject
    {
    Q_OBJECT
    Q_PROPERTY( int combGainI READ combGainI NOTIFY combGainIChanged )

    public:
    explicit Generator( QObject* parent = 0 );

    int combGainI() const { return m_combGainI; }
    

    public slots:
    void calculateNextValue();

    signals:
    void combGainIChanged();

    private:
    int m_combGainI;
    };

    #endif // GENERATOR_H
    @

    As you can see we declare a single member variable, m_combGainI, which we expose via the Q_PROPERTY macro as a read-only property. That is not to say that we can't change th evalue of the variable, just that its value can only be changed form within the class but not via the property system.

    Note that in accordance with the Q_PROPERTY statement we provide a getter function and a notifier signal that we must emit when the property changes.

    We also provide a handy function for the purposes of this example that allows a new value of the combGainI property to be calculated, calculateNextValue().

    Here is the implementation of the Generator class:

    @
    #include "generator.h"

    Generator::Generator( QObject* parent ) :
    QObject( parent ),
    m_combGainI( 0 )
    {
    }

    void Generator::calculateNextValue()
    {
    // Calculate a new value
    ++m_combGainI;

    // Let the world know this property has changed
    emit combGainIChanged();
    

    }
    @

    As you can see it is very simple. The calculateNextValue() function simply increments our member variable and then emits the appropriate property change notifier signal. Note that the signal implementation is provided by the moc during the build process.

    That is all there is to our Generator class. Of course in your real application the calculations will likely be much more complex than this but this is enough to give you an idea of how it all works.

    Now let's take a look at the Widget class. We have already layed out the ui for our Widget class consisting of a QLabel and a QDeclarativeView. Here is the declaration of the Widget class:

    @
    #ifndef WIDGET_H
    #define WIDGET_H

    #include <QWidget>

    namespace Ui {
    class Widget;
    }

    class Generator;

    class QTimer;

    class Widget : public QWidget
    {
    Q_OBJECT

    public:
    explicit Widget( QWidget* parent = 0 );
    ~Widget();

    public slots:
    void updateCombGainI();

    protected:
    void keyPressEvent( QKeyEvent* e );

    private:
    Ui::Widget ui;
    QTimer
    m_timer;
    Generator* m_generator;
    };

    #endif // WIDGET_H
    @

    This is a very simple class. We declare the ui member variable as per usual along with a pointer to a Generator object and a QTimer which is used to periodically call Generator::calculateNextValue().

    I have also overridden the virtual QWidget::keyPressEvent( QKeyEvent* ) function in this class. As you'll see below, the implementation of this function simply quits the running application when the user presses the escape key.



  • Here is the implementation of the Widget class:

    @
    #include "widget.h"
    #include "ui_widget.h"

    #include "generator.h"

    #include <QCoreApplication>
    #include <QDeclarativeContext>
    #include <QKeyEvent>
    #include <QTimer>

    Widget::Widget( QWidget* parent ) :
    QWidget( parent ),
    ui( new Ui::Widget ),
    m_timer( new QTimer( this ) ),
    m_generator( new Generator( this ) )
    {
    ui->setupUi( this );

    // Use a couple of connections to do something useful.
    
    // This one causes new data to be calculated once a second (once the timer is started)
    connect( m_timer, SIGNAL( timeout() ), m_generator, SLOT( calculateNextValue() ) );
    
    // This one causes the new data to update the GUI
    connect( m_generator, SIGNAL( combGainIChanged() ), this, SLOT( updateCombGainI() ) );
    
    // Get a pointer to the declarative view
    QDeclarativeView* view = ui->declarativeView;
    
    // We need to tell the declarative view about our C++ object that we wish to expose.
    // The exposed object and it's API form the interface with which our QML interacts.
    view->rootContext()->setContextProperty( "generator", m_generator );
    
    // Now load a qml file that binds to properties of this C++ object.
    view->setSource( QUrl( "qrc:/simple.qml" ) );
    
    // Start the timer off to kick things into motion
    m_timer->start( 1000 );
    

    }

    Widget::~Widget()
    {
    delete ui;
    }

    void Widget::updateCombGainI()
    {
    int combGainI = m_generator->combGainI();
    QString s = QString( "CombGainI = %1" ).arg( combGainI );
    ui->label->setText( s );
    }

    void Widget::keyPressEvent( QKeyEvent* e )
    {
    switch ( e->key() )
    {
    case Qt::Key_Escape:
    QCoreApplication::instance()->quit();
    break;

        default:
            QWidget::keyPressEvent( e );
    }
    

    }
    @

    Let's look at the parts that implement the QWidget-based part of the GUI first. In the constructor we call

    @ui->setupUi( this );@

    to instantiate the actual widgets that we layed out in designer. Then we make a couple of connections that cause the timer to trigger a call to Generator::calculateNextValue() and also calls the function Widget::updateCombGainI() when the Generator class emits its notifier signal:

    @
    // Use a couple of connections to do something useful.

    // This one causes new data to be calculated once a second (once the timer is started)
    connect( m_timer, SIGNAL( timeout() ), m_generator, SLOT( calculateNextValue() ) );

    // This one causes the new data to update the GUI
    connect( m_generator, SIGNAL( combGainIChanged() ), this, SLOT( updateCombGainI() ) );
    @

    And finally in the constructor we start off the timer:

    @
    m_timer->start( 1000 );
    @

    The other half of the QWidget-based GUI is to actually show the updates in the QLabel. This is done in the function

    @
    void Widget::updateCombGainI()
    {
    int combGainI = m_generator->combGainI();
    QString s = QString( "CombGainI = %1" ).arg( combGainI );
    ui->label->setText( s );
    }
    @

    Since we are using the property notifier signal which has no arguments we first need to query the Generator object for the new value. Then we construct a string using that value and set it as the text property of the label. All simple stuff and nothing new here.


Log in to reply