Menu

Main Menu
Talk Get Daily Search

Member's Online

    User Name
    Password

    Need assistance from QT programmer

    Reply
    Page 17 of 18 | Prev | 7   15     16   17   18   | Next
    Berserk | # 161 | 2010-10-02, 13:50 | Report

    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

    Edit | Forward | Quote | Quick Reply | Thanks

    Last edited by Berserk; 2010-10-04 at 18:52.
    The Following 2 Users Say Thank You to Berserk For This Useful Post:
    d-iivil, sixwheeledbeast

     
    Berserk | # 162 | 2010-10-02, 14:14 | Report

    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());

    Edit | Forward | Quote | Quick Reply | Thanks
    The Following 2 Users Say Thank You to Berserk For This Useful Post:
    d-iivil, sixwheeledbeast

     
    d-iivil | # 163 | 2010-10-10, 14:43 | Report

    Help! I'm once again in dead end

    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.

    Edit | Forward | Quote | Quick Reply | Thanks

     
    Berserk | # 164 | 2010-10-10, 15:56 | Report

    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
    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.

    Edit | Forward | Quote | Quick Reply | Thanks

    Last edited by Berserk; 2010-10-10 at 16:04.

     
    d-iivil | # 165 | 2010-10-10, 17:06 | Report

    Originally Posted by Berserk View Post
    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
    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

    Edit | Forward | Quote | Quick Reply | Thanks

     
    CepiPerez | # 166 | 2010-10-10, 17:15 | Report

    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.

    Edit | Forward | Quote | Quick Reply | Thanks

    Last edited by CepiPerez; 2010-10-10 at 17:17.

     
    d-iivil | # 167 | 2010-10-10, 17:43 | Report

    Originally Posted by CepiPerez View Post
    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

    Edit | Forward | Quote | Quick Reply | Thanks

    Last edited by d-iivil; 2010-10-10 at 17:46.

     
    d-iivil | # 168 | 2010-10-10, 18:38 | Report

    Got it working... didn't know I had to include also the "ui_dialog.." in my mainwindow.h


    Edit | Forward | Quote | Quick Reply | Thanks

     
    BLIZZARD | # 169 | 2010-11-19, 15:36 | Report

    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 ).

    Edit | Forward | Quote | Quick Reply | Thanks

     
    SubCore | # 170 | 2010-11-19, 15:46 | Report

    Originally Posted by BLIZZARD View Post
    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");

    Edit | Forward | Quote | Quick Reply | Thanks

    Last edited by SubCore; 2010-11-19 at 18:55. Reason: forgot an asterisk, it should be a pointer
    The Following 2 Users Say Thank You to SubCore For This Useful Post:
    BLIZZARD, nicolai

     
    Page 17 of 18 | Prev | 7   15     16   17   18   | Next
vBulletin® Version 3.8.8
Normal Logout