How to center a window on the screen
#include <QStyle> #include <QDesktopWidget> window->setGeometry(QStyle::alignedRect(Qt::LeftToRight, Qt::AlignCenter, window->size(), qApp->desktop()->availableGeometry() ));
#include <QStyle> #include <QDesktopWidget> window->setGeometry(QStyle::alignedRect(Qt::LeftToRight, Qt::AlignCenter, window->size(), qApp->desktop()->availableGeometry() ));
class MainWindow : public QMainWindow { Q_OBJECT; public: MainWindow(QWidget *parent = 0) : QMainWindow(parent) { QSettings settings; restoreGeometry(settings.value("mainWindowGeometry").toByteArray()); // create docs, toolbars, etc... restoreState(settings.value("mainWindowState").toByteArray()); } void closeEvent(QCloseEvent *event) { QSettings settings; settings.setValue("mainWindowGeometry", saveGeometry()); settings.setValue("mainWindowState", saveState()); } }; int main(int argc, char *argv[]) { QApplication a(argc, argv); QCoreApplication::setOrganizationDomain("OrgDomain"); QCoreApplication::setOrganizationName("OrgName"); QCoreApplication::setApplicationName("AppName"); QCoreApplication::setApplicationVersion("1.0.0"); MainWindow w; w.show(); return a.exec(); }
The above code will save and restore window position, size, toolbar visibility, toolbar docking area, dock states, locations and sizes. It saves using QSettings which will store your settings in a platform correct way.
if (fruit== "apple") { ... } // hidden malloc
if (fruit== QLatin1String("apple")) { ... } // fast and mentions encoding
if (foo.startsWith("(" + type + ") 0x"))
if (foo.startsWith("(" % type % ") 0x"))
The QtService component is useful for developing Windows services and Unix daemons.
The project provides a QtService template class that can be used to implement service applications, and a QtServiceController class to control a service.
On Windows systems the implementation uses the Service Control Manager.
On Unix systems services are implemented as daemons.
The QtSingleApplication component provides support for applications that can be only started once per user.
For some applications it is useful or even critical that they are started only once by any user. Future attempts to start the application should activate any already running instance, and possibly perform requested actions, e.g. loading a file, in that instance.
The QtSingleApplication class provides an interface to detect a running instance, and to send command strings to that instance.
For console (non-GUI) applications, the QtSingleCoreApplication variant is provided, which avoids dependency on QtGui.
QThread was designed and is intended to be used as an interface or a control point to an operating system thread, not as a place to put code that you want to run in a thread. We object-oriented programmers subclass because we want to extend or specialize the base class functionality. The only valid reasons I can think of for subclassing QThread is to add functionality that QThread doesn’t have, e.g. perhaps providing a pointer to memory to use as the thread’s stack, or possibly adding real-time interfaces/support. Code to download a file, or to query a database, or to do any other kind of processing should not be added to a subclass of QThread; it should be encapsulated in an object of it’s own.
// create the producer and consumer and plug them together Producer producer; Consumer consumer; bool bOk = producer.connect(&consumer, SIGNAL(consumed()), SLOT(produce())); Q_ASSERT(bOk); bOk = consumer.connect(&producer, SIGNAL(produced(QByteArray *)), SLOT(consume(QByteArray *))); Q_ASSERT(bOk); // they both get their own thread QThread producerThread; producer.moveToThread(&producerThread); QThread consumerThread; consumer.moveToThread(&consumerThread); // go! producerThread.start(); consumerThread.start();
Reference: Threading without the headache or QThread’s no longer abstract (see attached file)