[solved] adding Qt to existing program
OK...I've cleared the decks of my dumb mistakes (I think). I believe that I have now correctly implemented a class Soc, which contains a routine called runOneCycle() that replaces most of the functionality in my original main(). I now need to "hook it up" so it gets called. I guess the next step is to implement a version of the widget software that Zap posted above. Right now, I'd be happy if it ran 100 times, and showed the results of two variables after each running. (I have lots of plans for enhancing this, but this will do for now.)
I won't need a timer (yet), so we can eliminate that part of the code. So...do I need just one connect statement? Or, do I need two, since I'm planning on displaying the values of two variables with the Soc class?
Thanks...I think I'm slowly getting closer...
Are you able to post your code? If yes then please do, if not then we'll have to work with examples still. Not a problem either way...
I can post some extracts, which would hopefully be good enough. I removed some irrelevant stuff for brevity.
class Soc : public QObject
Filter filter; // Soc contains one filter for now.
- clocks and resets.
// bool systemReset;
- arguments to drive the filter processing.
int shaperOutI, shaperOutQ;
explicit Soc(QObject *parent = 0);
int getCoeffs(int *aI, int *aQ);
void dataChanged (double value);
#endif // SOC_H
And here's the .cpp file:
const int resetValue = 55;
//Soc::Soc(QObject *parent) :
Soc::Soc(QObject *parent) :
static int i=0;
- test a single filter.
// temporarily use simple variables for comb stuff.
combGainI = i;
combGainQ = i*2;
filter.cycle(combGainI, // run the high cycle.
- clocks and resets.
So...I've added the widget files from the example above to my program, and changed m_generator to a Soc variable called (creatively enough) soc. The program runs, and produces the correct output (as displayed on the console), but I'm not getting anything in my GUI window.
I believe I need to modify this line of code in widget.cpp:
@ connect (soc, SIGNAL(dataChanged(double)), this, SLOT(setValue(double)));
I imagine I'd have to modify the doubles to ints, but...I need to tell it to look at my two variables. Do I:
- need two connect statements for this?
- replace "this" with soc.member-name?
I guess I should also modify setValue to accept an int instead of a double, too.
Thje connect is correct. But I fI look at your last code, where do you emit the signal?
I guess I need a emit dataChanged() call, don't I?
So, my question now is...what do I put in as a parameter for this call? I guess this is the missing link to my understanding of how these signals work. (It also looks like I missed changing a double to int in the Soc.h file.)
you have to put a double in, as specified in the header.
Which one, I can't tell you, that's depending on your application.
Typically, the value you want to notify :-)
So, I use a double in the call even if the variable I want to display is an int? What exactly does that double represent: I thought it was the variable I wanted to display...
EDIT: I just put the dataChanged() call into my code, and it seems to work. Now, if I want to do this for two variables, what all do I have to duplicate? Obviously, I'll need two calls to dataChanged(), but what else do I have to do?
Think a bit, how the Qt widgets to it.
Look at QSpineBox:
- it has a signal valueChanged(int) and sends the changed value.
Look at QDoubleSpineBox:
- it has a signal valueChanged(double) and sends the changed value.
Look at QComboBox:
- void activated ( int index )
- void activated ( const QString & text )
both signals are emitted, with different content.
The client can connect to the signal he wants and has to interpret the values.
You can do a dataChanged() signal and leave it to the client to query all values and check which ones are changed, or do a dataChanged1() and dataChanged2() so the client knows, which value but has to query the new value. or you do
and deliver the new values to the client.
But I would rename dataChanged1(int) to some senseful names.
Which values are you trying to have displayed and what are their types? If you are emitting a single double then the signal, slot and connect are fine.
If you want to display 2 doubles then you need to modify:
- The dataChanged() signal to have two double arguments ie
@void dataChanged( double val1, double val2 );@
- The Widget::setValue() function to accept a corresponding pair of doubles:
@void setValue( double val1, double val2 );@
- The connect to use the new signal/slot signatures:
@connect (soc, SIGNAL(dataChanged(double,double)), this, SLOT(setValue(double,double)));@
Then finally you actually need to emit your signal from your calculation function.
If instead you are using int's then simply replace double for int in the above.
Thanks for the explanation, Gerolf...that does help.
Zap: I wish to display two ints. I understand now that I have to replace the doubles with ints. Now that I'm displaying two variables (ints), what is the syntax for forming the output string? I tried this:
@ QString s = QString("shaperOutI is %1. shaperOutQ is %2.").arg(shaperOutI, shaperOutQ);
But that gives no output at all. Do I need two separate strings? What if I wanted the output on separate lines?
Thanks. I'll have some more questions when I finish waking up.
QString s = QString("shaperOutI is %1. shaperOutQ is %2.").arg(shaperOutI).arg(shaperOutQ);
Gerolf has already shown you how to format the string. If you want it split over two lines then you need to include a "\n" in your string:
@QString s = QString("shaperOutI is %1.\nshaperOutQ is %2.").arg(shaperOutI).arg(shaperOutQ);@
You may need to set the wordWrap property of the QLabel to true too - I can't recall offhand.
Oh, this is OUTSTANDING. Thanks so much to everyone for their help in this thread. It may not seem like much to anyone else, but this is a major step forward for me.
A final question about all this: can someone explain this line:
@QString s = QString("shaperOutI is %1. shaperOutQ is 2.").arg(shaperOutI).arg(shaperOutQ);@
What's going on with the consecutive .arg calls?
I've got a ton of other questions now, but they're not apropos of this particular thread, so...I'll bundle them into a new thread. This one, I think we can mark solved (except I don't remember how to do that).
a qstring object has a method called arg which replaces %1 by its value and returns a temporary object. if on this temp object, another arg is called, it replaces all instances of %2 by its value and so on. Afaik, up to 99 :-)
So...how do I mark this solved? With a tag?
go to your first post, click edit and change the title :-)
Just add [Solved] in front
[quote author="Gerolf" date="1301503305"]a qstring object has a method called arg which replaces %1 by its value and returns a temporary object. if on this temp object, another arg is called, it replaces all instances of %2 by its value and so on. Afaik, up to 99 :-)[/quote]
The rationale behind the %1 %2 and so on is, that these strings are regularly translatable and the placeholders need not be integers or numbers but also strings. In some languages, things must be reordered to result in a valid sentence. So you can the write
QString x = QString("%2 is before %1).arg("first").arg("second");
The resulting string is "second is before first".
[quote author="mzimmers" date="1301501568"]Oh, this is OUTSTANDING. Thanks so much to everyone for their help in this thread. It may not seem like much to anyone else, but this is a major step forward for me.[/quote]
You're welcome. The initial steps are often the most confusing with a new technology. I will shortly post a modified example that performs the calculations in a separate thread and uses signals to let the main GUI thread know about the current status. That way you;ll have an example on which you can build if you do need to do some seriously time consuming number crunching without blocking the GUI.