Search code examples
c++qtqlineedit

store input of line edit to string qt


I'm a beginner.I am making a simple gui program using qt in which you enter a url/website and that program will open that webpage in chrome.I used line edit in which user enters url and i used returnPressed() slot, but the problem is (it might sound stupid) that i don't know how to take the input by user and store it in a string so that i can pass that string as parameter to chrome.Is im asking something wrong.also tell me how can i save input to a txt file, i know how to do that in a console program.Is this process is same with others like text edit etc.
My mainwindow.cpp:

    QString exeloc = "F:\\Users\\Amol-2\\AppData\\Local\\Google\\Chrome\\Application\\chrome.exe";

void MainWindow::on_site_returnPressed()
{
    QString site;
    getwchar(site);
    QString space=" ";
    QString result = exeloc + space + site;
    QProcess::execute(result);

}

What im doing wrong.
thanks


Solution

  • QLineEdit has a text() function that will return a QString. So you can do something like this:

    QString site = ui->site->text();
    

    You don't have to use QProcess to open a web site in a browser. You can use QDesktopServices::openUrl static function.

    Like this:

    QString site = ui->site->text();
    QUrl url(site);
    QDesktopServices::openUrl(url);
    

    Remember to include QDesktopServices and QUrl headers:

    #include <QDesktopServices>
    #include <QUrl>