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
我刚到QT,所以请原谅,如果我在这里做错了什么,但我已经看了所有的问题,但似乎找不到工作。我试着让用户通过输入一个名字来创建一个文件夹,然后它会创建一个名为的文件夹。我说“create”,因为它不是真正的创建,它会在你输入名字之前先做一个名为“project”的文件夹,当你输入一个名字时,它会重新命名它。但是,当我尝试重命名文件夹时,它给了我一个输入的名称。
error:C2664: 'int rename(const char *,const char *)' : cannot convert argument 1 from 'QString' to 'const char *'
错误:C2664:“int rename(const char *,const char *)”:不能将参数1从“QString”转换为“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.
如果你们能帮忙,我会很感激的,我已经被困在这里好几个小时了。
3 个解决方案
#1
0
Try dir.rename(dir.dirName(), name);
尝试dir.rename(dir.dirName()、名称);
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()
.
因为rename()是QDir的成员函数,所以您需要一个QDir实例来调用它。因此,不只是调用rename(),它调用谁知道什么,您需要dir.rename()。
QDir::rename()
actually takes 2 QString
s 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.
QDir: rename()实际上是将两个qstring作为参数,但是您调用的其他函数包含两个原始字符串,因此您不需要转换字符串,您只是调用了错误的函数。
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.
如果它仍然不起作用,那么您的输入参数要么是错误的,要么是有些东西阻止了操作系统重命名目录,例如当前正在使用的文件。
#2
1
You could call QString::toStdString(), and then call c_str() to retrieve the const char* from the std::string.
您可以调用QString::toStdString(),然后调用c_str()从std::string中检索const char*。
Your code would look something like this:
你的代码应该是这样的:
if (dir.exists()) {
rename(projectPath.toStdString().c_str(), name);
}
#3
-1
Qt FAQ says:
Qt FAQ说:
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();
}
#1
0
Try dir.rename(dir.dirName(), name);
尝试dir.rename(dir.dirName()、名称);
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()
.
因为rename()是QDir的成员函数,所以您需要一个QDir实例来调用它。因此,不只是调用rename(),它调用谁知道什么,您需要dir.rename()。
QDir::rename()
actually takes 2 QString
s 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.
QDir: rename()实际上是将两个qstring作为参数,但是您调用的其他函数包含两个原始字符串,因此您不需要转换字符串,您只是调用了错误的函数。
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.
如果它仍然不起作用,那么您的输入参数要么是错误的,要么是有些东西阻止了操作系统重命名目录,例如当前正在使用的文件。
#2
1
You could call QString::toStdString(), and then call c_str() to retrieve the const char* from the std::string.
您可以调用QString::toStdString(),然后调用c_str()从std::string中检索const char*。
Your code would look something like this:
你的代码应该是这样的:
if (dir.exists()) {
rename(projectPath.toStdString().c_str(), name);
}
#3
-1
Qt FAQ says:
Qt FAQ说:
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();
}