Unsolved How to open one time new widget
-
Hi all,
I want to open full screen widget one time.A new widget is opening each click .
fs *_fs = new fs(*this, 0); _fs->show();
I tried this code but doesn't work.
fs *_fs = new fs(*this, 0); if(!_fs->isVisible()) { _fs->show(); }
-
Hi! What exactly "doesn't work"? Please provide a minimal example of what you're doing, say what you expect the code to do and what it actually does for you. Btw, to show a widget in full screen mode you can use
void QWidget::showFullScreen()
. -
fs *_fs = new fs(*this, 0); _fs->show();
I used this code.But this code open a new widget every click.I want to open only one widget.How can I do that.
-
Sure, because with
new
you create a new widget everytime -
Hi
You should move the variable into the classclass mainwin : xx {
fs *_fs=NULL; // make sure its NULL
};then say
if (!_fs)
_fs = new fs(*this, 0);Note its VERY IMPORTANT its set to NULL to begin with or u will crash.
-
There's a design pattern called "Singleton" that describes how to deal with the issue of creating "just one instance of a class for the whole program" - mrjj already told you the most important things about how it can be implemented in C++, but you can read more about it taking a look at e.g.:
- Codeproject: Singleton Pattern & its implementation with C++
- MSDN: Exploring the Singleton Design Pattern
One remark regarding singleton and C++11: Since C++11 it is even possible to create a threadsafe singleton very easy:
class Singleton { public: static Singleton& getInstance() { static Singleton instance; return instance; } };
-
@raspe88 said in How to open one time new widget:
There's a design pattern called "Singleton"
Indeed, there is such an antipattern. But @asttekin, I ask you, I beg you, I implore you, forget it exists and never follow links leading to it! It is like the road to hell - all paved with good intentions.
-
@kshegunov @raspe88 Not to mention that this antipattern is an exorbitant overhead in this case! It is really just a matter of checking whether the pointer is null or not.