Wednesday, 15 September 2010

c++ - How to prevent open file twice? -



c++ - How to prevent open file twice? -

i have mdi application. application allowed open text files in subwindow. problem can open same file many times.

i have used next incomplete code:

qlist<qmdisubwindow*> subwindows = ui->mdiarea->subwindowlist()

but how can know if file exists or not.

how can prevent behavior ?

modification

i have doing next code, , works fine: (please review code)

qlist <qmdisubwindow*> subwindows = ui -> mdiarea -> subwindowlist(); if (subwindows.count() > 0) { (int = 0; < subwindows.count(); i++) { if (subwindows.at(i) -> property("filepath").tostring() == filedlg) { return; } } }

full code:

qstring filedlg = qfiledialog::getopenfilename(this, "open file", "f://", "text files(*.txt)"); if (filedlg != "") { // start of import code qlist < qmdisubwindow * > subwindows = ui -> mdiarea -> subwindowlist(); if (subwindows.count() > 0) { (int = 0; < subwindows.count(); i++) { if (subwindows.at(i) -> property("filepath").tostring() == filedlg) { return; } } } // end of import code qstring cache; qfile file(filedlg); if (file.open(qfile::readonly)) { qtextstream in ( & file); while (!file.atend()) { cache = in .readall(); } } file.close(); qfileinfo finfo(file); qwidget * widget = new qwidget(this); qtextedit * textedit = new qtextedit(widget); textedit -> setobjectname("mytextedit"); qmdisubwindow * mdiwindows = ui -> mdiarea - > addsubwindow(widget); mdiwindows -> setproperty("filepath", filedlg); mdiwindows -> setgeometry(5, 5, 300, 250); mdiwindows -> setwindowtitle(finfo.basename()); mdiwindows -> layout() -> addwidget(textedit); mdiwindows -> setwindowstate(qt::windowmaximized); mdiwindows -> layout() -> setspacing(0); textedit -> settext(cache); mdiwindows -> show(); }

depending on operating system, same file can have several different names. same name can referred in different ways (think traversing directory structure!). purely name-based scheme bound fail.

doing quite useful, have same file open several times, @ 1 place while editing another. allow user decide.

c++ qt qt5

No comments:

Post a Comment