[solved] adding Qt to existing program
-
I can post some extracts, which would hopefully be good enough. I removed some irrelevant stuff for brevity.
Here's Soc.h:
@#ifndef SOC_H
#define SOC_H#include <QObject>
#include <iostream>
#include <fstream>
#include <iomanip>
#include "DemodShaperFilter.h"class Soc : public QObject
{
Q_OBJECT
private:
Filter filter; // Soc contains one filter for now.
/*- clocks and resets.
/
bool clockEnable;
bool clockState;
// bool systemReset;
bool filterReset;
/ - arguments to drive the filter processing.
*/
int shaperCoeffI[NBR_CELLS];
int shaperCoeffQ[NBR_CELLS];
int combGainI;
int combGainQ;
int shaperOutI, shaperOutQ;public:
explicit Soc(QObject *parent = 0);
int getCoeffs(int *aI, int *aQ);signals:
void dataChanged (double value);
public slots:
void runOneCycle();
};
#endif // SOC_H
@And here's the .cpp file:
@#include "Soc.h"
const int resetValue = 55;//Soc::Soc(QObject *parent) :
// QObject(parent)
Soc::Soc(QObject *parent) :
QObject(parent), filter(resetValue)
{
}void Soc::runOneCycle()
{
static int i=0;
int rc;
/*- test a single filter.
*/
// temporarily use simple variables for comb stuff.
combGainI = i;
combGainQ = i*2;
i++;systemClock.setClock(HIGH);
filter.cycle(combGainI, // run the high cycle.
shaperCoeffI,
combGainQ,
shaperCoeffQ,
shaperOutI,
shaperOutQ,
clockEnable,
filterReset);return;
}
@ - 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.
Thanks.
-
Oops.
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.)
Thanks.
-
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?
Thanks.
-
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
- dataChanged1(int)
- dataChanged2(int)
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.
-
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).
Thanks again!
-
[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.