Simple Qt library to improve and help in their projects
For use all classes add in your project something like this:
QT += core gui webkitwidgets network
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = application
TEMPLATE = app
SOURCES += main.cpp
include($$PWD/vendor/qt-helper/qt-helper.pri)
Note: vendor is just a suggestion on how to organize your folders, to add third-party libraries:
sample/
├─── main.cpp
├─── mainwindow.cpp
├─── mainwindow.h
├─── mainwindow.ui
├─── sample.pro
└─── vendor/
└─── qt-helper/
├─── application/
│ ├─── oneinstanceapp.cpp
│ ├─── oneinstanceapp.h
│ ├─── oneinstanceapp.pri
│ ├─── proxystyle.cpp
│ ├─── proxystyle.h
│ └─── proxystyle.pri
│
├─── desktop/
│ ├─── openexternal.cpp
│ ├─── openexternal.h
│ ├─── openexternal.pri
│ ├─── shortcut.cpp
│ ├─── shortcut.h
│ ├─── shortcut.pri
│ ├─── trackmouse.cpp
│ ├─── trackmouse.h
│ └─── trackmouse.pri
│
├─── network/
│ ├─── networkmanager.cpp
│ ├─── networkmanager.h
│ └─── networkmanager.pri
│
└─── webkit/
├─── webglobals.cpp
├─── webglobals.h
└─── webglobals.pri
The classes in this scope are used to adjust or control anything related to your application, or QApplication, or add extra functionality.
This class allows your application to only have one instance. Usage example:
#include "mainwindow.h"
#include "oneinstanceapp.h"
int main(int argc, char *argv[])
{
OneInstanceApp app("instance", argc, argv);
MainWindow win;
win.show();
return app.exec();
}For include ony
OnceInstanceAppin your application put in your.pro, eg.:... SOURCES += main.cpp include($$PWD/vendor/qt-helper/application/oneinstanceapp.pri)
Adjust the style and improve the combobox popup:
#include "mainwindow.h"
#include "proxystyle.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
app.setStyle(new ProxyStyle());
MainWindow win;
win.show();
return app.exec("~instance");
}| Method | Description |
|---|---|
new ProxyStyle() |
Default theme |
new ProxyStyle("Fusion") |
Define Fusion theme in application |
new ProxyStyle("Windows") |
Define Windows theme in application |
new ProxyStyle("WindowsXP") |
Define Windows XP theme in application |
new ProxyStyle("WindowsVista") |
Define Windows Vista theme in application |
new ProxyStyle("Motif") |
Define Motif theme in application |
new ProxyStyle("CDE") |
Define CDE theme in application |
new ProxyStyle("Plastique") |
Define Plastique theme in application |
new ProxyStyle("Cleanlooks") |
Define Cleanlooks theme in application |
Note: Default themes depend on system availability or Qt version
For custom theme you can extends like this:
class CustomStyle : public ProxyStyle
{
Q_OBJECT
public:
CustomStyle();
...
}
...
app.setStyle(new CustomStyle());
...More details:
- Qt5: https://doc.qt.io/qt-5/qtwidgets-widgets-styles-example.html
- Qt6: https://doc.qt.io/qt-6.2/qtwidgets-widgets-styles-example.html
- Gallery: https://doc.qt.io/qt-6/gallery.html
For include ony
ProxyStylein your application put in your.pro, eg.:... SOURCES += main.cpp include($$PWD/vendor/qt-helper/aplication/proxystyle.pri)
Prevent freeze (or crash) apps with QDesktopServices::openUrl:
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow)
{
ui->setupUi(this);
QObject::connect(ui->btn1, SIGNAL(clicked()), this, SLOT(showFileInExplorer()));
QObject::connect(ui->btn2, SIGNAL(clicked()), this, SLOT(openFile()));
}
void MainWindow::showFileInExplorer()
{
OpenExternal::showInFolder("C:/foder/file.txt");
}
void MainWindow::openFile()
{
OpenExternal::open("C:/foder/file.txt");
}| Method | Description |
|---|---|
OpenExternal::open("C:/foo/bar/") |
Open default file manager (explorer.exe in Windows or Finder in macOS) |
OpenExternal::open("C:/foo/file.txt") |
Try open with default program, if failed try use OpenExternal::showInFolder |
OpenExternal::showInFolder("C:/foo/file.txt") |
In Windows is equivalent to explorer /select,C:\foder\file.txt command |
OpenExternal::showInFolder("/Users/sample/Desktop/foo.txt") |
In macOS is equivalent to open -R /Users/sample/Desktop/foo.txt command |
For include ony
OpenExternalin your application put in your.pro, eg.:... SOURCES += main.cpp include($$PWD/vendor/qt-helper/desktop/openexternal.pri)
These methods make it easier to add shortcuts to widgets. Using:
Shortcut::keys(widget, "Alt+F5", this, SLOT(...));Is equivalent to:
QAction *action = new QAction(widget);
QObject::connect(action, SIGNAL(triggered()), this, SLOT(...));
widget->addAction(action);
widget->setShortcuts(QKeySequence("Alt+F5"));Example:
#include "shortcut.h"
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow)
{
ui->setupUi(this);
Shortcut::keys(this, "Ctrl+w", this, SLOT(close()));
Shortcut::keys(this, "F12", this, SLOT(screenshot()));
}
void MainWindow::screenshot()
{
...
}| Method | Description |
|---|---|
Shortcut::bind(QWidget*, QObject*, char*) |
This makes it easier to create a shortcut with a slot and add it to a Widget |
Shortcut::keys(QWidget*, QKeySequence &shortcut, QObject*, char*) |
Define shortcut to widget with QKeySequence |
Shortcut::keys(QWidget*, QString, QObject*, char*, bool) |
Is equivalent to Shortcut::keys(QWidget*, QKeySequence(QString*), ...) |
Shortcut::keys(QWidget*, QList<QKeySequence> &shortcuts, QObject*, char*) |
Define a shortcut of QList<QKeySequence> |
Shortcut::keys(QWidget*, QKeySequence::StandardKey, QObject*, char*) |
Define a shortcut with a standard key. More details in: https://doc.qt.io/qt-6/qkeysequence.html#standard-shortcuts |
For include ony
OnceInstanceAppin your application put in your.pro, eg.:... SOURCES += main.cpp include($$PWD/vendor/qt-helper/desktop/shortcut.pri)
#include "trackmouse.h";
...
Sample::Sample(QWidget *parent) : QWidget(parent)
{
TrackMouse *track = new TrackMouse(this);
QObject::connect(track, SIGNAL(position(QPoint)), this, SLOT(capture(QPoint)));
track->setDelay(1000); // Set delay (default is 100 ms)
track->setWidget(this, true);
track->enable(true);
}
Sample::capture(const QPoint position)
{
qDebug() << position;
}For include ony
TrackMousein your application put in your.pro, eg.:... SOURCES += main.cpp include($$PWD/vendor/qt-helper/desktop/trackmouse.pri)
This class fix problems with "Response unknown" that occurs with some servers:
#include "networkmanager.h"
...
NetworkManager *manager = new NetworkManager;
manager->cookieJar(cookiejar); // Get default jar
// Set NetworkManager to QWebPage
ui->webView->page()->setNetworkAccessManager(manager);In requests for unknown schemes, you can implement a customized response or perform various actions, such as opening external programs:
NetworkManager *manager = new NetworkManager;
QObject::connect(manager, SIGNAL(unknownScheme(QString,QNetworkReply*)), this, SLOT(myImplemetation(QString,QNetworkReply*)));
void MainWindow::myImplemetation(const QString &scheme, QNetworkReply *reply)
{
if (scheme == "tel") {
// Open phone application (can try uses `QDesktopServices::openUrl(reply.url())`)
} else if (scheme == "custom") {
// custom reply
}
}For include ony
NetworkManagerin your application put in your.pro, eg.:... SOURCES += main.cpp include($$PWD/vendor/qt-helper/network/networkmanager.pri)
Adjustments for QtWebkit made easy:
WebGlobals configs();
configs.developer(true); // Enable developer tools| Method | Description |
|---|---|
void WebGlobals::developer(const bool enable) |
Enable or disable developer tools |
QWebSettings *WebGlobals::configs() |
Same as QWebSettings::globalSettings() |
void WebGlobals::setStyle(const QString path) |
Specifies the location of a user stylesheet to load with every web page |
void WebGlobals::setFont(const int size, const QString font) |
Sets the font size and sets the actual font family to family for the specified generic family, which |
QIcon WebGlobals::getIcon(const QString url) |
Get icon from URL |
QString WebGlobals::getPath(const WebData type) |
Get full path from your browser profile data |
QString WebGlobals::getPath(WebData::All) |
Get path your browser profile |
QString WebGlobals::getPath(WebData::AppCache) |
Get cache path from your browser profile |
QString WebGlobals::getPath(WebData::LocalStorage) |
Get localstorage path from from your browser profile |
QString WebGlobals::getPath(WebData::OfflineStorage) |
Get offline path from from your browser profile |
QString WebGlobals::getPath(WebData::Icons) |
Get icons path from from your browser profile |
QString WebGlobals::getPath(WebData::Temporary) |
Get temporary path from your browser profile |
bool WebGlobals::erase(const WebData type) |
Erase data by type from your browser profile data |
bool WebGlobals::erase(WebData::All) |
Erase all data from your browser profile data |
bool WebGlobals::erase(WebData::AppCache) |
Erase cache from your browser profile data |
bool WebGlobals::erase(WebData::LocalStorage) |
Erase localstorage data from your browser profile data |
bool WebGlobals::erase(WebData::OfflineStorage) |
Erase offline data from your browser profile data |
bool WebGlobals::erase(WebData::Icons) |
Erase icons data from your browser profile data |
bool WebGlobals::erase(WebData::Temporary) |
Erase temporary data your browser profile data |
For include ony
WebGlobalsin your application put in your.pro, eg.:... SOURCES += main.cpp include($$PWD/vendor/qt-helper/web/webglobals.pri)
The sample APIs are located at ./sample, you can open the "sample.pro" file in QtDesigner, or compile via the command line.
Deploy in debug mode with command line:
cd /home/foo/bar/qt-helpder/sample
qmake "CONFIG += console warn_on debug" sample.pro
make
./debug/sample
For release:
cd /home/foo/bar/qt-helpder/sample
qmake "CONFIG += release" sample.pro
make
./release/sample
