Skip to content

Instantly share code, notes, and snippets.

@andr1972
Created April 9, 2022 12:46
Show Gist options
  • Save andr1972/317f6fa28e6d40b443645ee8b5509125 to your computer and use it in GitHub Desktop.
Save andr1972/317f6fa28e6d40b443645ee8b5509125 to your computer and use it in GitHub Desktop.
Start creating Qt compoenent
#include "dialog.h"
#include <QVBoxLayout>
#include <qxviewer.h>
#include <QLineEdit>
#include <QPushButton>
Dialog::Dialog()
{
QVBoxLayout *mainLayout = new QVBoxLayout;
QxViewer *widget = new QxViewer;
//widget->setBaseSize(400,400);
QHBoxLayout *hLayout = new QHBoxLayout;
hLayout->addWidget(new QLineEdit);
hLayout->addWidget(new QPushButton);
mainLayout->addLayout(hLayout);
mainLayout->addWidget(widget);
//widget.show();
setLayout(mainLayout);
mainLayout->setMargin(3);
resize(400,400);
}
#ifndef DIALOG_H
#define DIALOG_H
#include <QDialog>
class Dialog : public QDialog
{
public:
Dialog();
};
#endif // DIALOG_H
#include "qxviewer.h"
#include <QApplication>
#include <dialog.h>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
Dialog dialog;
dialog.show();
return app.exec();
}
#include "qxviewer.h"
#include <QPainter>
#include <QKeyEvent>
QxViewer::QxViewer(QWidget *parent) :
QWidget(parent)
{
setWindowTitle(tr("Viewer"));
#if QT_CONFIG(cursor)
setCursor(Qt::IBeamCursor);
#endif
}
void QxViewer::paintEvent(QPaintEvent * /* event */)
{
QPainter painter(this);
painter.fillRect(rect(), Qt::black);
painter.setPen(*(new QColor(255,34,255,255)));
painter.drawRect(15,15,100,100);
painter.setPen(Qt::white);
painter.drawText(rect(), Qt::AlignCenter, tr("Text on center"));
}
#if QT_CONFIG(wheelevent)
void QxViewer::wheelEvent(QWheelEvent *event)
{
}
#endif
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QScrollBar>
class QxViewer : public QWidget
{
Q_OBJECT
QScrollBar vscroll;
QScrollBar hscroll;
public:
explicit QxViewer(QWidget *parent = nullptr);
protected:
void paintEvent(QPaintEvent *event) override;
// void resizeEvent(QResizeEvent *event) override;
// void keyPressEvent(QKeyEvent *event) override;
#if QT_CONFIG(wheelevent)
void wheelEvent(QWheelEvent *event) override;
#endif
// void mousePressEvent(QMouseEvent *event) override;
// void mouseMoveEvent(QMouseEvent *event) override;
// void mouseReleaseEvent(QMouseEvent *event) override;
private:
// void scroll(int deltaX, int deltaY);
};
#endif // WIDGET_H
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment