-2

I am beginner in Qt, my question is about move the file to another directory. Here is my code:

std::string file_orign = "/home/joshua/text.txt";
std::string file_target = "/home/joshua/test";

std::string mv_command =  "mv "+file_orign+" "+file_target;
system(mv_command.c_str());

This one can work, but I need to get the file path and folder path from the text broswer, so I changed it.

std::string file_orign = ui->textBrowser1->toPlainText();
std::string file_target = ui->textBrowser2->toPlainText();

std::string mv_command =  "mv "+file_orign+" "+file_target;
system(mv_command.c_str());

Then the error is that I need conversion from 'QString' to non-scalar type 'std::__cxx11::string. So I want to consult you all is there any qt function can work like system move function? I try the QDir::rename function but I did not find a format like

std::string mv_command =  "mv "+file_orign+" "+file_target;
system(mv_command.c_str());

Another way is convert QString to std::string, but I think the first way is better. Hope someone can help me, thank you!

Sorry guys, all are my wrong, I asked a so unclear and so stupid question, I am so sorry for wasting your time, I am a beginner, before I asked this question, I really really had not noticed that this question was about the conversion from std::string to QString, because my level is to low. Because I am too naive, I can not ask question anymore, so please, please forgive me asked this question, I am too stress because I internship at a company, my project for me is quite hard so that I have no choice to do such a wasting your time thing, lastly, I want to say thank you for those who had seen my questions before.

innocent boy
  • 315
  • 4
  • 13
  • 2
    [QDir::rename()](http://doc.qt.io/qt-5.6/qdir.html#rename) – O'Neil Jul 25 '17 at 01:52
  • @eyllanesc come on dude, it is you again, yes I ask the same question but this is my thinking, my case and my problem, this is the third time I ask a question and you think I did not do any research. come on man, what you want to do? please undo the -1 ok? I just want to give all of you another way of how to move the file not only in Qt but also in C++, come on man, you really offend me. – innocent boy Jul 25 '17 at 04:14
  • Then Sir do not use as a Qt tag, do not mention it, you are not giving a new form, you are showing that you have a conversion error. – eyllanesc Jul 25 '17 at 04:18
  • I will continue to comment, even if you do not want to, we are in SO and here are rules. – eyllanesc Jul 25 '17 at 04:19
  • You could put a more descriptive title, for example: `Error: 'QString' to non-scalar type 'std::__cxx11::string when moving files` – eyllanesc Jul 25 '17 at 04:22
  • Please think that many do the search for solutions for the titles, and if this is inappropriate, we waste time. I do not put it down negatively, just put it probably a duplicate question. – eyllanesc Jul 25 '17 at 04:25
  • do not use as a Qt tag, I am doing project on Qt although now I have known this question is about the type conversion thanks to your answer but before you answer I really don't know this, I really think you offend me, yes, you are really batter than me on programming, you must know the feeling of the process from a beginner to a master, please think about it ok? – innocent boy Jul 25 '17 at 04:30
  • My comments are respectful, I think I make a constructive criticism since I support my opinion. My intention has never been to offend. We must all learn to receive criticism, here is a professional opinion. – eyllanesc Jul 25 '17 at 04:33
  • I commented that you read the following link: https://stackoverflow.com/help/how-to-ask, so that you can ask a more precise question, and we can help you, help us a lot because we take less time to understand it. Think that we are not doing the software, we are like detective looking for the reason. – eyllanesc Jul 25 '17 at 04:36
  • ok dude, thank you so much, I have learnt more not only how to solve this question. thanks again – innocent boy Jul 25 '17 at 04:37
  • @m7913d ok ok I have known already, do not minus marks ok ? you mean I need to delete this question correct? speechless – innocent boy Jul 27 '17 at 06:38
  • @m7913d I can not delete the question now, all in all, my level is too low to ask question at here. Bye :) – innocent boy Jul 28 '17 at 02:06
  • @eyllanesc hey bro, I can not ask any question now, please help me, because my new question is about using push button to change the displaying images which from a specific folder. – innocent boy Jul 28 '17 at 02:28
  • @m7913d because of you, I can not ask question anymore, I am going to be dismissed, thank you for your whole family. – innocent boy Jul 28 '17 at 02:32
  • Read this post: https://meta.stackexchange.com/questions/86997/what-can-i-do-when-getting-we-are-no-longer-accepting-questions-answers-from-th – m7913d Jul 28 '17 at 07:52

2 Answers2

0

In your case is a problem of conversion, for this you must convert QString to std::string, for it uses the function toStdString() . In your case it changes to the following:

std::string file_orign = ui->textBrowser1->toPlainText().toStdString()
std::string file_target = ui->textBrowser2->toPlainText().toStdString()
std::string mv_command =  "mv "+file_orign+" "+file_target;
system(mv_command.c_str());

If you are looking for a Qt-like solution, I recommend you use my previous answer:

QFile file(ui->textBrowser1->toPlainText());
file.rename(ui->textBrowser2->toPlainText());
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
-1

I suggest using the QProcess class http://doc.qt.io/qt-5/qprocess.html

QString cmd = "mv";
QString file_orign = "/home/joshua/text.txt";
QString file_target = "/home/joshua/test";
QStringList arg;
arg << file_orign << file_target;
QProcess cmdProcess;
cmdProcess.start(cmd, arg);
  • Welcome to StackOverflow! For help on writing good answers visit [How to answer](https://stackoverflow.com/help/how-to-answer) –  Jul 25 '17 at 03:18