1

I'm new to Qt and I'm working on a search application which search and list all videos in a folder. I want to generates clickable file paths which can run the video file when cilcked. here is the code I'm trying to do:

void MainWindow::on_pushButton_7_clicked()
{
QString key = ui->key->toPlainText();     //get the search keyword
QString text = ui->DIR->toPlainText();    //get the directory

string cmd = "./search_by_keyword.sh "+text.toStdString()+" "+key.toStdString();                          
                                          //command to run a shell script with 2 arguments
system("chmod +x search_by_keyword.sh");
system(cmd.c_str());                      //run the script which produce a txt file containing the search results

ifstream fin { "search_result.txt" };     //read result and print out
string s;
QStringList all;

while(!fin.eof()){
getline(fin,s);
string convert = s;                       //I want each "s" produced to be clickable, and it run the video in the path when clicked
QString str = convert.c_str();
all << str;
}

fin.close();
ui->searchResult->setText(all.join("\n")); //searchResult is currently a textBrowser

}

here is an scrshot of the UI, just in case: enter image description here

thank you!

Baodoan
  • 63
  • 1
  • 4
  • 1
    What is you question exactly? How to make lines in 'search result' clickable in UI or how to start player when a line is clicked? To start player you can use QDesktopServices::openUrl(QUrl(QString("file:///" + filePath))); . To make each line clickable. It looks like you use QTextEdit for "search result". Set its 'acceptRichText' property to true and format it so that lines will represent clickable links – mvidelgauz Jun 17 '16 at 19:17
  • My question is actually how to do both of those. I want the paths printed out to be clickable and when user click on it, it runs the video the path point to. Thank for your suggestion, i'll try if I can figure it out. – Baodoan Jun 18 '16 at 06:46
  • actually, opening the video files is easy as I can use system() function to run. But I just dont know how to make each line to become a button to execute the function. – Baodoan Jun 18 '16 at 06:55
  • You want push button or "clickable text" like links in this page (IMHO better for this kind of control, but that's your choice)? BTW you are using Qt mixed with os specific calls. Once with Qt, you can make your program fully portable, but that's again your choice... – mvidelgauz Jun 18 '16 at 07:58
  • clickable text would be better. but I accept anything, as long as it runs. I'm thinking of using html for the text, but it doesn't seem like html can run onclick function (or it's just that I dont know) – Baodoan Jun 18 '16 at 09:08

1 Answers1

1

Construct QUrl and add it to your reach text. Read more here - doc.qt.io/qt-5/qurl.html and here - doc.qt.io/qt-5/qtwidgets-draganddrop-dropsite-example.html. Also read this SO question and answer - stackoverflow.com/questions/16581425/…

Community
  • 1
  • 1
mvidelgauz
  • 2,176
  • 1
  • 16
  • 23