playing around with qt to find the correct ui approach

This commit is contained in:
Marcel Otte 2015-03-22 19:41:59 +01:00
commit 0ea4430252
8 changed files with 327 additions and 0 deletions

26
editorelement.cpp Normal file
View File

@ -0,0 +1,26 @@
#include "editorelement.h"
#include <QBoxLayout>
EditorElement::EditorElement(QWidget *parent) : QWidget(parent)
{
this->layout = new QBoxLayout(QBoxLayout::TopToBottom, this);
this->setLayout(layout);
label = new QLabel(this);
hint = new QLabel(this);
edit = new QLineEdit(this);
layout->addWidget(label);
layout->addWidget(hint);
layout->addWidget(edit);
label->setText("test");
hint->setText("another test");
edit->setText("content test");
}
EditorElement::~EditorElement()
{
}

28
editorelement.h Normal file
View File

@ -0,0 +1,28 @@
#ifndef EDITORELEMENT_H
#define EDITORELEMENT_H
#include <QBoxLayout>
#include <QObject>
#include <QWidget>
#include <QtGui>
#include <QLineEdit>
#include <QLabel>
class EditorElement : public QWidget
{
Q_OBJECT
public:
explicit EditorElement(QWidget *parent = 0);
~EditorElement();
private:
QBoxLayout*layout;
QLabel * label;
QLabel * hint;
QLineEdit* edit;
signals:
public slots:
};
#endif // EDITORELEMENT_H

150
flowlayout.cpp Normal file
View File

@ -0,0 +1,150 @@
#include <QtWidgets>
#include "flowlayout.h"
FlowLayout::FlowLayout(QWidget *parent, int margin, int hSpacing, int vSpacing)
: QLayout(parent), m_hSpace(hSpacing), m_vSpace(vSpacing)
{
setContentsMargins(margin, margin, margin, margin);
}
FlowLayout::FlowLayout(int margin, int hSpacing, int vSpacing)
: m_hSpace(hSpacing), m_vSpace(vSpacing)
{
setContentsMargins(margin, margin, margin, margin);
}
FlowLayout::~FlowLayout()
{
QLayoutItem *item;
while ((item = takeAt(0)))
delete item;
}
void FlowLayout::addItem(QLayoutItem *item)
{
itemList.append(item);
}
int FlowLayout::horizontalSpacing() const
{
if (m_hSpace >= 0) {
return m_hSpace;
} else {
return smartSpacing(QStyle::PM_LayoutHorizontalSpacing);
}
}
int FlowLayout::verticalSpacing() const
{
if (m_vSpace >= 0) {
return m_vSpace;
} else {
return smartSpacing(QStyle::PM_LayoutVerticalSpacing);
}
}
int FlowLayout::count() const
{
return itemList.size();
}
QLayoutItem *FlowLayout::itemAt(int index) const
{
return itemList.value(index);
}
QLayoutItem *FlowLayout::takeAt(int index)
{
if (index >= 0 && index < itemList.size())
return itemList.takeAt(index);
else
return 0;
}
Qt::Orientations FlowLayout::expandingDirections() const
{
return 0;
}
bool FlowLayout::hasHeightForWidth() const
{
return true;
}
int FlowLayout::heightForWidth(int width) const
{
int height = doLayout(QRect(0, 0, width, 0), true);
return height;
}
void FlowLayout::setGeometry(const QRect &rect)
{
QLayout::setGeometry(rect);
doLayout(rect, false);
}
QSize FlowLayout::sizeHint() const
{
return minimumSize();
}
QSize FlowLayout::minimumSize() const
{
QSize size;
QLayoutItem *item;
foreach (item, itemList)
size = size.expandedTo(item->minimumSize());
size += QSize(2*margin(), 2*margin());
return size;
}
int FlowLayout::doLayout(const QRect &rect, bool testOnly) const
{
int left, top, right, bottom;
getContentsMargins(&left, &top, &right, &bottom);
QRect effectiveRect = rect.adjusted(+left, +top, -right, -bottom);
int x = effectiveRect.x();
int y = effectiveRect.y();
int lineHeight = 0;
QLayoutItem *item;
foreach (item, itemList) {
QWidget *wid = item->widget();
int spaceX = horizontalSpacing();
if (spaceX == -1)
spaceX = wid->style()->layoutSpacing(
QSizePolicy::PushButton, QSizePolicy::PushButton, Qt::Horizontal);
int spaceY = verticalSpacing();
if (spaceY == -1)
spaceY = wid->style()->layoutSpacing(
QSizePolicy::PushButton, QSizePolicy::PushButton, Qt::Vertical);
int nextX = x + item->sizeHint().width() + spaceX;
if (nextX - spaceX > effectiveRect.right() && lineHeight > 0) {
x = effectiveRect.x();
y = y + lineHeight + spaceY;
nextX = x + item->sizeHint().width() + spaceX;
lineHeight = 0;
}
if (!testOnly)
item->setGeometry(QRect(QPoint(x, y), item->sizeHint()));
x = nextX;
lineHeight = qMax(lineHeight, item->sizeHint().height());
}
return y + lineHeight - rect.y() + bottom;
}
int FlowLayout::smartSpacing(QStyle::PixelMetric pm) const
{
QObject *parent = this->parent();
if (!parent) {
return -1;
} else if (parent->isWidgetType()) {
QWidget *pw = static_cast<QWidget *>(parent);
return pw->style()->pixelMetric(pm, 0, pw);
} else {
return static_cast<QLayout *>(parent)->spacing();
}
}

37
flowlayout.h Normal file
View File

@ -0,0 +1,37 @@
#ifndef FLOWLAYOUT_H
#define FLOWLAYOUT_H
#include <QLayout>
#include <QRect>
#include <QStyle>
class FlowLayout : public QLayout
{
public:
explicit FlowLayout(QWidget *parent, int margin = -1, int hSpacing = -1, int vSpacing = -1);
explicit FlowLayout(int margin = -1, int hSpacing = -1, int vSpacing = -1);
~FlowLayout();
void addItem(QLayoutItem *item) Q_DECL_OVERRIDE;
int horizontalSpacing() const;
int verticalSpacing() const;
Qt::Orientations expandingDirections() const Q_DECL_OVERRIDE;
bool hasHeightForWidth() const Q_DECL_OVERRIDE;
int heightForWidth(int) const Q_DECL_OVERRIDE;
int count() const Q_DECL_OVERRIDE;
QLayoutItem *itemAt(int index) const Q_DECL_OVERRIDE;
QSize minimumSize() const Q_DECL_OVERRIDE;
void setGeometry(const QRect &rect) Q_DECL_OVERRIDE;
QSize sizeHint() const Q_DECL_OVERRIDE;
QLayoutItem *takeAt(int index) Q_DECL_OVERRIDE;
private:
int doLayout(const QRect &rect, bool testOnly) const;
int smartSpacing(QStyle::PixelMetric pm) const;
QList<QLayoutItem *> itemList;
int m_hSpace;
int m_vSpace;
};
#endif // FLOWLAYOUT_H

11
main.cpp Normal file
View File

@ -0,0 +1,11 @@
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}

27
mainwindow.cpp Normal file
View File

@ -0,0 +1,27 @@
#include "mainwindow.h"
#include "../QtGuidedEditor/qgewidget.h"
#include "flowlayout.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
list = new QList<EditorElement*>();
editor = new QWidget(this);
centralLayout = new FlowLayout(editor);
editor->setLayout(centralLayout);
centralLayout->addWidget(new EditorElement(this));
centralLayout->addWidget(new EditorElement(this));
this->setCentralWidget(editor);
QString style = QString(
"QWidget {border: 2px dashed black;"
"border-radius: 5px;"
"background: gray }"
);
editor->setStyleSheet(style);
}
MainWindow::~MainWindow()
{
}

23
mainwindow.h Normal file
View File

@ -0,0 +1,23 @@
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QList>
#include "editorelement.h"
#include "flowlayout.h"
class MainWindow : public QMainWindow
{
Q_OBJECT
private:
FlowLayout* centralLayout;
QWidget* editor;
QList<EditorElement*>* list;
public:
MainWindow(QWidget *parent = 0);
~MainWindow();
};
#endif // MAINWINDOW_H

25
networkpacketcomposer.pro Normal file
View File

@ -0,0 +1,25 @@
#-------------------------------------------------
#
# Project created by QtCreator 2015-01-06T11:56:33
#
#-------------------------------------------------
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = networkpacketcomposer
TEMPLATE = app
SOURCES += main.cpp\
mainwindow.cpp \
editorelement.cpp \
flowlayout.cpp
HEADERS += mainwindow.h \
editorelement.h \
flowlayout.h
INCLUDEPATH += $$PWD/../QtGuidedEditor
DEPENDPATH += $$PWD/../QtGuidedEditor