QT窗口没有访问另一个类对象



我正在尝试制作一个文件管理系统。我是QT的新手,无法理解是什么导致了这个问题。任何帮助都会很感激。我有一个文件系统类,我想在window2中使用(window2实际上是我的主窗口)

//These are just some of methods of my FileSystem class
//Check if Folder exists
bool FileSystem:: folderExists(const Folder * pointerToCurrentFolder, string folderName) {
//code if folder exists or not
}
//Create Folder
void FileSystem::createFolder(Folder * pointerToParentFolder, string fName) {
//code to create a folder
}

我有一个window2类,我在window2中使用FileSystem的实例。我还包括文件系统头类在window2。

private:
FileSystem fs;
public:
void setFileSystem(FileSystem f){
fs=f;
}
FileSystem getFileSystem(){
return fs;
}

我想使用这个fs实例在第三窗口(名称:MainWindow)通过使用window2

private:
window2* w2;
public:
void setWindow2(window2* wt){
w2=wt;
}
window2* getWindow2(){
return w2;
}

我尝试在MainWindow中访问在window2中创建的fs对象,像这样:

void MainWindow::on_createAFolderBt_clicked() //create a folder command
{
bool ok;
QString fname=QInputDialog::getText(this,"File Management System", "Type name of Folder",QLineEdit::Normal, QDir::home().dirName(),&ok);
string newfolderName= fname.toLocal8Bit().constData(); //convert QString to std::string
if (ok){
if (w2->getFileSystem().folderExists(w2->getFileSystem().navigator,newfolderName)){
QMessageBox::warning(this,"","This Folder already exists");
}
}

每当我按下输入对话框的ok时,我的应用程序停止工作,当我调试它时,它显示了c++的stl::vector中的错误'Read Access Violation'。我在文件系统代码中多次使用向量。是因为当窗口关闭时文件系统对象被破坏了吗?

因此,根据我的理解,你正试图创建一个在主窗口中定义的文件系统对象,当它超出范围时,它会被销毁,但如果你想要它在你的应用程序中FileSystem,你可以使它成为一个单例类。

其次,你正在使用std字符串和向量,它将调度容器每次它分配内存,你需要使用qt容器,他们的方式更快速和优化qt框架。

试一试,它会解决你的问题。

最新更新