Skip to content

Instantly share code, notes, and snippets.

@sawaYch
Created January 8, 2020 16:06
Show Gist options
  • Save sawaYch/f18d0832ce4620d2c32d17837465b26c to your computer and use it in GitHub Desktop.
Save sawaYch/f18d0832ce4620d2c32d17837465b26c to your computer and use it in GitHub Desktop.
qt5 (cpp) : how to center the QApp.
#include "app.h"
#include "ui_app.h"
app::app(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::app)
{
ui->setupUi(this);
connect(this->ui->hashButton, SIGNAL (released()), this, SLOT (handleHashButton()));
this->ui->input->setFocus();
hash_algo_list += {"SHA256","SHA1","SHA224","SHA384", "SHA512", "MD4","MD5"};
this->ui->hash_cb->addItems(hash_algo_list);
this->center();
}
void app::center(){
// geometry of the main window
QRect qr = this->frameGeometry();
// center point of screen
QScreen *qc = QGuiApplication::primaryScreen();
QPoint cp = qc->geometry().center();
// move rectangle's center point to screen's center point
qr.moveCenter(cp);
// top left of rectangle becomes top left of window centering it
this->move(qr.topLeft());
}
void app::handleHashButton(){
QByteArray origin = this->ui->input->text().toUtf8();
QByteArray hashed;
switch (this->ui->hash_cb->currentIndex()) {
case 0:
hashed = QCryptographicHash::hash(origin, QCryptographicHash::Sha256);
break;
case 1:
hashed = QCryptographicHash::hash(origin, QCryptographicHash::Sha1);
break;
case 2:
hashed = QCryptographicHash::hash(origin, QCryptographicHash::Sha224);
break;
case 3:
hashed = QCryptographicHash::hash(origin, QCryptographicHash::Sha384);
break;
case 4:
hashed = QCryptographicHash::hash(origin, QCryptographicHash::Sha512);
break;
case 5:
hashed = QCryptographicHash::hash(origin, QCryptographicHash::Md4);
break;
case 6:
hashed = QCryptographicHash::hash(origin, QCryptographicHash::Md5);
break;
}
this->ui->hash_result->setText(hashed.toHex());
}
app::~app()
{
delete ui;
}
void app::on_input_returnPressed()
{
this->handleHashButton();
}
void app::on_hash_cb_currentTextChanged()
{
if (!(this->ui->input->text() == ""))
this->handleHashButton();
}
#ifndef APP_H
#define APP_H
#include <QMainWindow>
#include <QDesktopWidget>
#include <QScreen>
#include <QCryptographicHash>
#include <QDebug>
QT_BEGIN_NAMESPACE
namespace Ui { class app; }
QT_END_NAMESPACE
class app : public QMainWindow
{
Q_OBJECT
public:
app(QWidget *parent = nullptr);
~app();
private slots:
void handleHashButton();
void on_input_returnPressed();
void on_hash_cb_currentTextChanged();
void center();
private:
Ui::app *ui;
QStringList hash_algo_list;
};
#endif // APP_H
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment