qt/LedOK/gqt.h

89 lines
2.0 KiB
C
Raw Normal View History

2022-01-20 10:08:17 +08:00
#ifndef GQT_H
#define GQT_H
#include <QLayout>
#include <QFont>
#include <QThread>
template<class T>
class Wrp{
public:
T *obj;
Wrp(T *obj = nullptr){
this->obj = obj;
};
inline Wrp& operator()(T *obj){
this->obj = obj;
return *this;
}
inline Wrp& operator()(T *obj, QLayout *layout){
this->obj = obj;
layout->addWidget(obj);
return *this;
}
inline Wrp& addTo(QLayout *layout){
layout->addWidget(obj);
return *this;
}
inline Wrp& margin(int a){
obj->setMargin(a);
return *this;
}
inline Wrp& font(const QFont &font){
obj->setFont(font);
return *this;
}
inline Wrp& font(int size){
QFont font = obj->font();
font.setPointSize(size);
obj->setFont(font);
return *this;
}
inline Wrp& width(int w){
obj->setFixedWidth(w);
return *this;
}
inline Wrp& height(int h){
obj->setFixedHeight(h);
return *this;
}
inline Wrp& padding(int wAdd, int hAdd, int minW = 32, int minH = 16){
wAdd+=8;
hAdd+=8;
QSize size = obj->fontMetrics().size(Qt::TextShowMnemonic, obj->text());
int &rwidth = size.rwidth();
rwidth += wAdd;
if(rwidth < minW) rwidth = minW;
int &rheight = size.rheight();
rheight += hAdd;
if(rheight < minH) rheight = minH;
obj->setFixedSize(size);
return *this;
}
inline Wrp& alignC(){
obj->setAlignment(Qt::AlignCenter);
return *this;
}
inline Wrp& alignR(){
obj->setAlignment(Qt::AlignRight);
return *this;
}
inline Wrp& connStart(){
QObject::connect(obj, &QThread::finished, obj, &QThread::deleteLater);
obj->start();
return *this;
}
};
template <typename Func>
inline QThread *ThreadStart(Func &&f) {
QThread* thread = QThread::create(f);
QObject::connect(thread, &QThread::finished, thread, &QThread::deleteLater);
thread->start();
return thread;
}
#endif