【发布时间】:2013-10-23 13:36:16
【问题描述】:
我想做一个简单的应用程序:
在线编辑用户应该给出他/她想要在工作目录中创建的目录的路径(路径应该总是这样:./dirname - 现在我不处理任何错误,假设一切正常) 并且当他/她单击确定按钮时,应该创建一个名为 'dirname' 的目录。
但是当我通过路径时,可以说“./testdir”并单击“确定”,我的应用程序退出并显示“创建目录中的错误”,当然它不会创建目录。出了什么问题以及如何解决这个问题?
我正在使用 Qt Creator 2.8.1 基于 Windows XP 上的 Qt 5.1.1(MSVC 2010,32 位)。
代码如下:
主窗口.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
Ui::MainWindow *ui;
public slots:
void createdir();
};
#endif // MAINWINDOW_H
主窗口.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
#include "Windows.h"
#include <stdio.h>
#include <iostream>
std::string GetLastErrorStdStr()
{
DWORD error = GetLastError();
if (error)
{
LPVOID lpMsgBuf;
DWORD bufLen = FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
error,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR) &lpMsgBuf,
0, NULL );
if (bufLen)
{
LPCSTR lpMsgStr = (LPCSTR)lpMsgBuf;
std::string result(lpMsgStr, lpMsgStr+bufLen);
LocalFree(lpMsgBuf);
return result;
}
}
return std::string();
}
static const wchar_t *GetWC(const char *c)
{
const size_t cSize = strlen(c)+1;
wchar_t* wc = new wchar_t[cSize];
mbstowcs (wc, c, cSize);
return wc;
}
LPCWSTR castPath(const char *path)
{
WCHAR str3[1024];
MultiByteToWideChar( 0,0, path, strlen(path), str3, strlen(path)+1);
LPCWSTR cstr4 = str3;
return cstr4;
}
static void makeDir(const char *path)
{
if(CreateDirectory(castPath(path), NULL) == 0)
{
printf("ERROR IN CREATEDIRECTORY\n");
std::cout << GetLastErrorStdStr() << "\n";
exit(-1);
}
}
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
connect(ui->pushButton, SIGNAL(clicked()), this, SLOT(createdir()));
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::createdir()
{
makeDir(ui->lineEdit->text().toStdString().c_str());
}
main.cpp:
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
ui 文件:
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>300</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralWidget">
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QLineEdit" name="lineEdit"/>
</item>
<item>
<widget class="QPushButton" name="pushButton">
<property name="text">
<string>OK</string>
</property>
</widget>
</item>
</layout>
</widget>
<widget class="QMenuBar" name="menuBar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>20</height>
</rect>
</property>
</widget>
<widget class="QToolBar" name="mainToolBar">
<attribute name="toolBarArea">
<enum>TopToolBarArea</enum>
</attribute>
<attribute name="toolBarBreak">
<bool>false</bool>
</attribute>
</widget>
<widget class="QStatusBar" name="statusBar"/>
</widget>
<layoutdefault spacing="6" margin="11"/>
<resources/>
<connections/>
</ui>
【问题讨论】:
-
如果你只传递
'testdir'作为创建目录的路径,是否有效? -
@vahancho:不,它不起作用。我刚刚想出了一个新想法——我认为这可能是问题所在。当我在 VisualStudio 上构建一些应用程序时,有时我必须将“字符集”从“使用 Unicode 字符集”更改为“使用多字节字符集”以使其正常工作。会不会是这里的问题?如何在 QtCreator 中更改“字符集”?
-
您是否阅读了
CreateDirectory的文档。它告诉您如何在出现错误时获取更多信息。我建议你听从这个建议。 -
@DavidHeffernan:当然我试过了。我修改后的代码:pastie.org/private/ah0uccx0gux8bbcx1vpmzg 但实际上并没有多大帮助...
-
除此之外,演员阵容完全是假的。到底是什么让你这么做的?!您正在使用 C++ 进行编码,因此如果您必须进行转换,请使用 C++ 样式转换并了解您在做什么。但是当编译器告诉你你传递了错误类型的参数时,你不应该忽略它!!!