maemo.org - Talk

maemo.org - Talk (https://talk.maemo.org/index.php)
-   Development (https://talk.maemo.org/forumdisplay.php?f=13)
-   -   Need assistance from QT programmer (https://talk.maemo.org/showthread.php?t=58569)

Berserk 2010-10-02 13:50

Re: Need assistance from QT programmer
 
Ok, here's how to read values from a terminal command (or any other process)
It doesn't use system(), but it runs a process with QProcess.

You can still use system() for running processes without worrying about output.
Maybe it's possible to catch the output of system(), but I don't know how yet.


In myclass.h:
Code:

#include <QProcess>

private:
    QProcess myProcess;
    void startProcess();

public slots:
    void readOutput();

In myclass.cpp:
Code:

#include <QDebug>

    // The signal 'readyReadStandardOutput' is emitted when the process has made new data available through its standard output channel (stdout)
    connect(&myProcess, SIGNAL(readyReadStandardOutput()),
            this, SLOT(readOutput()));

void myClass::startProcess() {
    // The process itself (starting)
    myProcess.start("gconftool-2 -R /apps/osso/hildon-desktop/views", QProcess::ReadOnly);   
}

void myClass::readOutput() {
    // Put the output in a QString
    QString outData = myProcess.readAllStandardOutput();
   
    qDebug() << outData;
}

You can stop a QProcess with myProcess.close(), connect it to a button or signal or something.
Trolltech: "Closes all communication with the process and kills it."

In all cases, I recommend putting myProcess.close() in the destructor of the class. Maybe it's already closed because the parent is destroyed, but I like to be sure about this :)

More info: http://doc.trolltech.com/4.7-snapshot/qprocess.html
You can also change the openMode for the process, but in this case, ReadOnly is fine.



EDIT: please note when using .start()
You can use
myProcess.start(QString process, QStringList arguments, OpenMode)
or
myProcess.start(QString process and arguments, OpenMode)
If you put the processname and arguments in 1 QString, like I did in the above example, then please be aware that all arguments are separated by spaces. You need to use quotes around arguments that contain spaces, more info:
http://doc.trolltech.com/4.7-snapshot/qprocess.html

Berserk 2010-10-02 14:14

Re: Need assistance from QT programmer
 
By the way, in the above example, everytime the process outputs new data, the QString is overwritten with the new data.

If you'd like to catch the entire output, then use:
Code:

QString outData;
outData.append(myProcess.readAllStandardOutput());


d-iivil 2010-10-10 14:43

Re: Need assistance from QT programmer
 
Help! I'm once again in dead end :D

I have a button on my mainwindow which opens up a dialog when pressed. This dialog has it's own header, ui and cpp -files and it does some stuff on it's own. That's working just fine. On that dialog I have one button and when it's pressed, I'd like to send message back to the mainwindow and launch function that will change stuff in mainwindow with parameter taken from the dialog and then close the dialog.

No matter what I do, I can't figure out how to make that happen :(

I'm a bit lost because I don't what to google for. I found some examples which makes it possible to call for function, but on those examples the function would only alter the dialog window, not the mainwindow.

Berserk 2010-10-10 15:56

Re: Need assistance from QT programmer
 
I think you should make the connects in your QMainWindow class, for instance:

Code:

testDialog = new QDialog(this);

connect(testDialog->testButton, SIGNAL(clicked()),
        testDialog, SLOT(close()));

connect(testDialog->testButton, SIGNAL(clicked()),
        this, SLOT(yourFunction()));

EDIT:
About the parameter from the dialog:
I don't know if this is the right way to do it, but it should work.

You can make a variable protected, so that it's also accessible for friends. Making friends in C++, yay :D
Code:

class yourDialog {
    friend class yourMainWindow;
   
protected:
    int parameter;
}

Then you can access it with testDialog->parameter, where "testDialog" is an instance of the above yourDialog class.

d-iivil 2010-10-10 17:06

Re: Need assistance from QT programmer
 
Quote:

Originally Posted by Berserk (Post 837762)
I think you should make the connects in your QMainWindow class, for instance:

Code:

testDialog = new QDialog(this);

connect(testDialog->testButton, SIGNAL(clicked()),
        testDialog, SLOT(close()));

connect(testDialog->testButton, SIGNAL(clicked()),
        this, SLOT(yourFunction()));

EDIT:
About the parameter from the dialog:
I don't know if this is the right way to do it, but it should work.

You can make a variable protected, so that it's also accessible for friends. Making friends in C++, yay :D
Code:

class yourDialog {
    friend class yourMainWindow;
   
protected:
    int parameter;
}

Then you can access it with testDialog->parameter, where "testDialog" is an instance of the above yourDialog class.

Thanks, I already open the dialog with the method you described, but I can't reach the buttons (or any other elements) on the dialog's UI :(

CepiPerez 2010-10-10 17:15

Re: Need assistance from QT programmer
 
In dialog.h add this in public section:
QString getParam() { return param; }

In dialog.cpp add this to the button function:
param = "whatever qstring you want"

In Mainwindow.cpp add this after dialog->exec():
QString myParam = dialog->getParam();


Another easy way is make the dialgo button to store a config value in a .conf file. Then when you close the dialgo you can read this value usin qsettings.

d-iivil 2010-10-10 17:43

Re: Need assistance from QT programmer
 
Quote:

Originally Posted by CepiPerez (Post 837805)
In dialog.h add this in public section:
QString getParam() { return param; }

In dialog.cpp add this to the button function:
param = "whatever qstring you want"

In Mainwindow.cpp add this after dialog->exec():
QString myParam = dialog->getParam();


Another easy way is make the dialgo button to store a config value in a .conf file. Then when you close the dialgo you can read this value usin qsettings.

Right now I store the string to be passed in a regular QString which is set public in dialog.h. I can access that from my mainwindow.cpp with no problems.

But what I can't reach is the button from dialog.ui so I could connect it to the slot in mainwindow.cpp

In my mainwindow.h I have put this:
Code:

protected:
dialog* diagWin;

And in dialog.h I have
Code:

class dialog : public QDialog
{
    Q_OBJECT

    friend class MainWindow;

And in mainwindow.cpp I open the dialog by:
Code:

diagWin = new dialog(this);
diagWin->show();

All that is working, but why I cannot reach any ui-elements of dialog.ui by typing:
diagWin->ui->MyButtonName

Qt Creator autofills this far: diagWin->ui
But after that I cannot access anything inside the UI :(

d-iivil 2010-10-10 18:38

Re: Need assistance from QT programmer
 
Got it working... didn't know I had to include also the "ui_dialog.." in my mainwindow.h

:)

BLIZZARD 2010-11-19 15:36

Re: Need assistance from QT programmer
 
I thought it is better to put here my little QT question in stead of creating a hole new thread, so here it comes:

Lets say i have 10 labels named label_1,label_2, label_3.......label_10 and i would like to change the text to "something".

I want something like this:

for (number=1; number<=10; number++)
ui->label_number->setText("something");

but i have broblem at "label_number" part, i also tried label_/number , with that way it recognises the variable but i get a different error (i also convertet number to string but then i get another differrent error :().

SubCore 2010-11-19 15:46

Re: Need assistance from QT programmer
 
Quote:

Originally Posted by BLIZZARD (Post 878004)
for (number=1; number<=10; number++)
ui->label_number->setText("something");

i don't think it's possible to use dynamic variable names in c++ ( i could be wrong, though...)

if you want to iterate over several labels, it's probably easiest to put them into some kind of container, i.e.

Code:

map<int, QLabel *> Labels;
Labels[1] = ui->label_1;
Labels[2] = ui->label_2;

for (number=1; number<=10; number++)
Labels[number]->setText("asdf");



All times are GMT. The time now is 15:07.

vBulletin® Version 3.8.8