Question

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

Was it helpful?

Solution 2

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>

OTHER TIPS

You've got your approach slightly wrong, I can see where you're coming from though. It's actually a lot more simple than you're trying, Qt has a QDesktopServices class that allows you to interact with various system items, including open urls in the browser. There's documentation on it here.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top