0

I'm new to QT so please excuse me if I'm blatantly doing something wrong here, but I've looked at all the questions here on the matter but can't seem to find something that works. I'm trying to have the user create a folder by entering a name for it, and it 'creates' a folder with the name. I say 'create' because it's not exactly creating one, it makes a folder first called "project" before you enter the name, and when you enter a name it renames it. However, when I try and rename the folder with the inputted name it gives me

error:C2664: 'int rename(const char *,const char *)' : cannot convert argument 1 from 'QString' to 'const char *'

Here's my code:

    void MainWindow::on_actionNew_Project_triggered(const char *parameter)
{
    //Create project folder
    QString projectPath = "D:/Project";

    QDir dir(projectPath);
        if (!dir.exists()) {
            dir.mkpath(projectPath);
    }


    //Get project name from user
    bool result;
    QString name = QInputDialog::getText(0, "New Project",
                                             "Enter in project name", QLineEdit::Normal,
                                             "", &result);
    if(result && !name.isEmpty()) {

    //Rename project folder to user created name
    QDir dir(projectPath);
        if (dir.exists()) {
            rename(projectPath, name); //Gives me error HERE

        }
    }
}

I would appreciate it if you guys could help, I've been stuck on this for hours.

2
  • 1
    This question does not contain an MCVE.
    – MrEricSir
    Mar 19, 2017 at 5:31
  • 2
    As the compiler error states, you need two const char* arguments, but your projectPath and name is of type QString. projectPath.toLatin1().data() could be used as argument. Next time you should include all relevant function declarations i.e. your rename method in this case in order being able to help you faster.
    – bkausbk
    Mar 20, 2017 at 9:15

3 Answers 3

7

You could call QString::toStdString(), and then call c_str() to retrieve the const char* from the std::string.

Your code would look something like this:

if (dir.exists()) {
        rename(projectPath.toStdString().c_str(), name);
    }
2

Try dir.rename(dir.dirName(), name);

You are trying to invoke a member function without an instance.

Since rename() is a member function of QDir, you need a QDir instance in order to invoke it. So rather than just calling rename() which invokes who knows what, you need to dir.rename().

QDir::rename() actually takes 2 QStrings as parameters, but that other function you are invoking takes two raw strings, so you don't really need to convert the strings, you were just calling the wrong function.

bool QDir::rename(const QString & oldName, const QString & newName)

You are most likely calling rename() from <stdio.h>, which could also work given that the parameters are correct and the OS can rename the directory, in that case you will need to convert to "raw" C-style strings via yourString.toLatin1().constData(). But since you are using Qt, you might as well use the QDir API, which works directly with QString.

If it still doesn't work, then either your input parameters are wrong, or there is something preventing the OS from rename the directory, for example a file currently in use.

6
  • Well, it stopped giving me the error, but it doesn't rename the folder
    – user7733800
    Mar 19, 2017 at 4:17
  • @GuyWhoNeedsHelp - that's a different issue. This question is about converting QString to const char *. If it doesn't work as expected, then there is something else wrong with your code.
    – dtech
    Mar 19, 2017 at 4:18
  • Seems like you have some more learning to do, not only about Qt but also about C++ in general.
    – dtech
    Mar 19, 2017 at 4:23
  • See the last edit, if that still doesn't work, then debug your code. Check your paths and so on. Just because previously it didn't compile doesn't mean that now execution reaches there. You might be passing the wrong path. There are a number of things that can go wrong, you need to find out what.
    – dtech
    Mar 19, 2017 at 4:46
  • @dtech: Although your solution would be correct in itself, this is not an answer to the question of the questioner. His problem is that he has not shown all the relevant parts of the source code to answer his question correctly. So we can only speculate how his own "rename" method looks like.
    – bkausbk
    Mar 20, 2017 at 9:20
-1

Qt FAQ says:

int main(int argc, char **argv)
{
 QApplication app(argc, argv);
 QString str1 = "Test";
 QByteArray ba = str1.toLatin1();
 const char *c_str2 = ba.data(); 
 printf("str2: %s", c_str2);
 return app.exec();
}
5
  • I get "error: C2039: 'toAscii' : is not a member of 'QString'" when I do that
    – user7733800
    Mar 19, 2017 at 4:06
  • 1
    There is no longer toAscii() in Qt, toLatin1() is the correct function.
    – dtech
    Mar 19, 2017 at 4:07
  • So try below the section 1 in the answer i says there : toLatin1();
    – EAK TEAM
    Mar 19, 2017 at 4:07
  • It gives me the error again. Here is my code after doing what you guys said: QString a("Asdf"); const char *c = a.toLatin1(); //Rename project folder to user created name QDir dir(projectPath); if (dir.exists()) { rename(projectPath, a);
    – user7733800
    Mar 19, 2017 at 4:09
  • I basically did "QString a("Asdf"); const char *c = a.toAscii();" but with toLatin1()
    – user7733800
    Mar 19, 2017 at 4:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.