QObject::connect: No such slot error
-
Isn't that the job of your
curefile
variable ? -
What do you mean by "A typo" ?
-
What do you mean by "A typo" ?
I'd used curefile rather than curFile!
I tested the program and it's fine with both close and exit now. Now there remain two problems:
How to handle the Windows red X button?
And the built-in closeAllWindows() slot (connected to the exit button), closes all windows when clicked, but when I create an installer exactly using the program's .exe file, that exit button only closes its file not all files! -
Then you can re-implemnet the closeEvent function to do the removal before proceeding further.
-
@SGaist
Done! Thank you, handy help about closeEvent.When I create an installer and install the app on my Windows machine, the open button works as expected and doesn't open the opened files again but using double click I can open the same files as many times as I like.
Here is main.cpp:
#include <QApplication> #include <QSplashScreen> #include "mainwindow.h" int main(int argc, char* argv[]) { QApplication app(argc, argv); QSplashScreen* splash = new QSplashScreen; splash->setPixmap(QPixmap(":/images/splash.jpg")); splash->show(); Qt::Alignment topRight = Qt::AlignRight | Qt::AlignTop; splash->showMessage(QObject::tr("Setting up the main window..."), topRight, Qt::white); MainWindow* mainWin = new MainWindow; if(argc > 1) mainWin->loadFile(argv[1]); mainWin->show(); splash->finish(mainWin); delete splash; return app.exec(); }
It calls loadFile:
bool MainWindow::loadFile(const QString &fileName) { if(already_Opened(fileName)) return false; else vf.push_back(fileName); if (!spreadsheet->readFile(fileName)) { statusBar()->showMessage(tr("Loading canceled"), 3000); return false; } setCurrentFile(fileName); statusBar()->showMessage(tr("File loaded"), 3000); return true; }
Here is already_Opened also:
bool MainWindow::already_Opened(const QString &fileName) { for(int i=0; i<vf.size(); ++i) if(fileName == vf[i]) return true; return false; }
vf
is a vector of QString. What can be the problem in your point of view? -
Likely that you are opening your application several times.
Also, you're not destroying your MainWindow object properly.
I don't see why you need to use a static variable for your vector.
Also, you should take a look at the documentation of the class you are using, you are reimplementing by hand a method that already exists in QVector. See contains.
-
Also, you should take a look at the documentation of the class you are using, you are reimplementing by hand a method that already exists in QVector. See contains.
Thanks, good method. I used it.
Likely that you are opening your application several times.
Also, you're not destroying your MainWindow object properly.
I don't see why you need to use a static variable for your vector.
I think the issue belongs to running the program by double clicking, so that a double click runs the program and opens a file and the next double click runs the program once again and opens that file again and so on.
I declared the vector static so that there is only one vector for any execution.Incidentally, the program uses the statements:
MainWindow* mainWin = new MainWindow; mainWin->show();
two times in main.cpp and newFile():
#include <QApplication> #include <QSplashScreen> #include "mainwindow.h" int main(int argc, char* argv[]) { QApplication app(argc, argv); QSplashScreen* splash = new QSplashScreen; splash->setPixmap(QPixmap(":/images/splash.jpg")); splash->show(); Qt::Alignment topRight = Qt::AlignRight | Qt::AlignTop; splash->showMessage(QObject::tr("Setting up the main window..."), topRight, Qt::white); MainWindow* mainWin = new MainWindow; if(argc > 1) mainWin->loadFile(argv[1]); mainWin->show(); splash->finish(mainWin); delete splash; return app.exec(); }
void MainWindow::newFile() { MainWindow* mainWin = new MainWindow; mainWin->show(); }
Also, for destroying the objects when closed, I use,
setAttribute(Qt::WA_DeleteOnClose);
in the Mainwindow's constructor. -
Then if you don't want that to happen, then you have to ensure that there's only one instance of your application running using for example the QtSingleApplication class.
Why use a new MainWindow for every document ? The editor itself should rather be a dedicated widget.
-
Then if you don't want that to happen, then you have to ensure that there's only one instance of your application running using for example the QtSingleApplication class.
Does it mean that the user will have only one file opened at the same time? The user may need to open various files and compare, say, the data of two of them at the same time.
Why use a new MainWindow for every document ?
Do you mean new MainWindow in newFile() slot or main?
Please take a look at the explanations of the book about these:
https://www.imageupload.co.uk/images/2017/09/12/131a64.png
https://www.imageupload.co.uk/images/2017/09/12/25f435.png
https://www.imageupload.co.uk/images/2017/09/12/3.png
https://www.imageupload.co.uk/images/2017/09/12/4.pngThe book has meant an SDI, and for that it has suggested a foreach loop. I didn't understand that loop well and couldn't use it so went for employing a vector.
I'm still rather astonished by this behaviour:
The double click sends the name of the file to the loadFile slot so if it's previously opened, the first if statement closes the function and the program execution doesn't reach the function spreadsheet->readFile(fileName) and the file shouldn't open once again, but in effect, it opens again. Isn't it strange?The editor itself should rather be a dedicated widget.
Editor?
(Sorry For the Delay)
-
@tomy said in QObject::connect: No such slot error:
connect(closeAction, SIGNAL(triggered(bool)), this, SLOT(close()));
connect(closeAction, SIGNAL(triggered(bool)), this, SLOT(remove_File(curefile)));As always in threads related to signals/slots, I must jump in to advocate the Qt5 syntax:
connect(closeAction, &QAction::triggered, this, &MainWindow::close);
because compile time connection checks are awesome :-)
-
No, I mean having one instance of your application handling more than one file.
What book is that ?
No it's not strange, you are expecting your static variable to be the same across all instances of your application, that's wrong.
The editor: the widget used to edit your document.
-
No, I mean having one instance of your application handling more than one file.
It's likely the best choice. If I'm right I think it's related to MDI (Multiple Document Interface) which is what the book pointed to alongside that foreach loop, that I didn't understand well and not sure where, in what functions in the project, to write that loop.
What book is that ?
C++-GUI-Programming-with-Qt-4-2nd Edition
No it's not strange, you are expecting your static variable to be the same across all instances of your application, that's wrong.
The static variable is the same between instances of the application if we've created new instances using the menus and buttons. But each time we open new files using double clicking the event is different and it's as though we are executing the app for the first time. Disagree?
-
You are mixing "instance of an application" with "instance of an object in an application".
See the documentation about static.
-
@SGaist said in
You are mixing "instance of an application" with "instance of an object in an application".
The main class of the project is MainWindow. We create an instance of the class (an object using new) in tow functions, main() and newFile(). So when we run the program by the IDE or its icon (when installed) and manipulate the data, save it, open a file by the Open button, or make a new file by the New button, we have one instance of the application but may have two or more instances of the class. So in these cases our static data member is the same.
But when we run our program using a double click on a stored file, we have an instance of the program, and if we open another, again by a double click, we have another instance of the program and these two are as two whole and independent instances and are "not" connected together so that the static data member is the same. And that's why we can open the same file as many times as we wish using double clicking. I think you agree now.My assumption: we can't prevent this unwanted event by manipulating the code of the program unless that manipulation is regarding the operating system to gain assistance from it to tell us if there is a file running at the time being and we decide not to open it again.
-
@tomy In that case I would suggest something like QSharedMemory.
Check in your Main.cpp if an other instance of your programm is already up and running. If it is, pass the File path to your other instance and exit main.