Set editline text to path?



  • So I have 2 GUIS, one directory browser and one main GUI, on the main GUI there's a line edit. Whenever I go to directory browser, select a directory and then click a button, the path should appear on the line edit, but it doesn't. It doesnt, can someone please check what I did wrong?

    from directoryexplorer.cpp
    @void directoryExplorer::on_selectButton_clicked()
    {
    if(ui->treeView->selectionModel()->hasSelection()){
    QModelIndex i = ui->treeView->selectionModel()->currentIndex();
    QString path = mmodel->fileInfo(i).absoluteFilePath();
    MainWindow o;
    o.fillInOutput(path);
    }
    }@

    from mainwindow.cpp
    @void MainWindow::fillInOutput(QString fpath){
    ui->lineEditOutput->setText(fpath);
    }
    @


  • Moderators

    Of course it doesn't work like that.
    @
    MainWindow o; //o is a local variable, not your main window
    o.fillInOutput(path);
    } //o is destroyed here.
    @
    This sounds like a job for signals and slots. In your directoryExplorer header add signal:
    @signals:
    void pathChanged(QString);
    @
    and replace the code above with
    @emit pathChanged(path);
    @
    In the main window connect to that signal:
    connect(<yourDirExplorerPointer>, directoryExplorer::pathChanged, this, MainWindow::fillInOutput);



  • [quote author="Chris Kawa" date="1397316003"]Of course it doesn't work like that.
    @
    MainWindow o; //o is a local variable, not your main window
    o.fillInOutput(path);
    } //o is destroyed here.
    @
    This sounds like a job for signals and slots. In your directoryExplorer header add signal:
    @signals:
    void pathChanged(QString);
    @
    and replace the code above with
    @emit pathChanged(path);
    @
    In the main window connect to that signal:
    connect(<yourDirExplorerPointer>, directoryExplorer::pathChanged, this, MainWindow::fillInOutput);[/quote]

    Thanks but I don't understand the last instruction. I wrote
    connect(<de>, directoryExplorer::pathChanged, this, MainWindow::fillInOutput);

    because I'm sure that's my pointer, is it not? I wrote it in the header like this : directoryExplorer de;


  • Moderators

    I meant <yourDirExplorerPointer> as a placeholder, sorry.

    connect() expects a pointer so in your case, since you have a stack variable de, you need to pass its address:
    @connect(&de, directoryExplorer::pathChanged, this, MainWindow::fillInOutput);@

    The call to connect should be placed in an initializing function of your MainWindow eg. in the constructor.

    I think it would be good for you to get back to some C++ basics before you start with Qt. It's only going to get harder. Without knowing what pointers are or what a variable scope will be you're gonna struggle a lot.



  • [quote author="Chris Kawa" date="1397323784"]I meant <yourDirExplorerPointer> as a placeholder, sorry.

    connect() expects a pointer so in your case, since you have a stack variable de, you need to pass its address:
    @connect(&de, directoryExplorer::pathChanged, this, MainWindow::fillInOutput);@

    The call to connect should be placed in an initializing function of your MainWindow eg. in the constructor.

    I think it would be good for you to get back to some C++ basics before you start with Qt. It's only going to get harder. Without knowing what pointers are or what a variable scope will be you're gonna struggle a lot.[/quote]

    Yeah probably, I watched most of thenewboston's tutorials (skipped the last 15) but I keep on forgetting about everything, I should probably pick up a book rather than watching videos



  • Chris: I think you need to get the pointer of the class member function for the connect to work?
    @
    &directoryExplorer::pathChanged
    @
    instead of
    @
    directoryExplorer::pathChanged
    @
    ?

    I've also told glowdemon1 to learn the c++ basics first (in another thread), it will certainly help. and I think you should also learn how to interpret compiler errors, because most of your problems are syntax errors and show during compilation.

    Btw: how can i escape the at-sign in this forum? lol


  • Moderators

    @Xander84 - yeah, of course, sorry for that. My mind was somewhere else when I typed that.

    The formatting on this forum is the most mind-boggling thing ever.
    It's suppose to be "textile":http://qt-project.org/wiki/TextileSyntax but not really :P
    To use @ in your text you can either have just one in the whole text or use html entity: &#64;
    Also, if you use @ inside a code tag without whitespace like this:
    @a@b@c@ it will use the outer ones for code and the inner ones as text. If you have couple of them next to each other neighbored by a character eg. a@@@ it will print them all.
    I would love to see the code for that parser one day :P



  • Thanks for the formatting explanation, I would expect to just write @@ to "escape" it or something easy but it didn't work. :D


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.