QLayout 对齐小部件错误
QLayout aligning widget as wrong
我有 QLayout
class 接受 QLabel
并将其对齐为 Qt::AlignRight
在单元格中,最初我认为它正在工作(因为布局单元格的大小是正好等于像素图的大小),我有一个与 QLabel
关联的事件,即当 mousepressEvent
发生时 QLabel
的大小增加(单元格的大小也增加,因此整个列的大小增加),那时 QLayout
中的其他 QLabel
左对齐,我希望它们右对齐或居中对齐而不是左对齐,
我的代码是,
Container::Container()
{
Layout = new QGridLayout;
Layout->setHorizontalSpacing(0);
Layout->setVerticalSpacing(10);
Layout->setMargin(10);
for(int i = 0; i < 4; ++i)
{
holes[i] = new Hole;
Layout->addWidget(ui_holes[i], i, 0, 1, Qt::AlignRight);
ui_holes[i].setPixmap("mypixmapname.png")
}
Layout->setAlignment(Qt::AlignCenter);
setLayout(Layout);
setMaximumSize(200,760);
setMinimumSize(200,760);
setSizePolicy( QSizePolicy::Fixed, QSizePolicy::Fixed);
}
void Screen::mousePressEvent(QMouseEvent *tevent)
{
if(childAt(tevent->pos()))
{
if(childAt(tevent->pos())->objectName() == "Hole")
{
hole = static_cast<Hole *>(childAt(tevent->pos()));
hole->resize(QSize(160,160));
}
}
}
void Screen::mouseReleaseEvent(QMouseEvent*)
{
if(hole)
{
ui_Hole->resetSize();
}
}
Hole 是 class 继承自 QLabel 并且我为 Hole 创建了两个新的成员函数是 resetSize 和 resize,
void Hole::resize(QSize size)
{
setSize(size);
if(!ui_HoleFlags[PIXMAP_EXISTS])
return void(0);
QPixmap *tempPixmap = ui_resourceIcon();
setPixmap(tempPixmap->scaled(size,Qt::IgnoreAspectRatio));
delete tempPixmap;
}
QPixmap* Hole::ui_resourceIcon()
{
if(!ui_HoleFlags[ICON_EXISTS])
return NULL;
QPixmap *tempPixmap = new QPixmap(*pixmap());
return tempPixmap;
}
void Hole::setSize(QSize size)
{
setMaximumSize(size);
setMinimumSize(size);
}
void Hole::resetSize()
{
if(ui_HoleFlags[PIXMAP_EXISTS])
setPixmap(*Pixmap);
setSize(ICON_SIZE);
}
提前致谢
这样,无论单个标签的大小如何,您都可以使标签始终居中对齐。
#include <QLabel>
#include <QApplication>
#include <QLayout>
#include <QMouseEvent>
class Window : public QWidget
{
public:
Window(QWidget *parent = 0) {}
virtual ~Window() {}
protected:
void mousePressEvent(QMouseEvent *event)
{
if (childAt(event->pos())) {
QLabel *label = dynamic_cast<QLabel *>(childAt(event->pos()));
if (label) {
label->setMinimumSize(QSize(50, 50));
label->setMaximumSize(QSize(50, 50));
}
}
}
};
int main(int argc, char **argv)
{
QApplication app(argc, argv);
Window *window = new Window;
window->show();
window->resize(400, 300);
QVBoxLayout *vLayout = new QVBoxLayout;
QLabel *labels[4];
for(int i = 0; i < 4; ++i) {
labels[i] = new QLabel();
labels[i]->setMaximumSize(30, 30);
labels[i]->setMinimumSize(30, 30);
QHBoxLayout *hLayout = new QHBoxLayout;
hLayout->addItem(new QSpacerItem(10, 10, QSizePolicy::Expanding, QSizePolicy::Minimum));
hLayout->addWidget(labels[i]);
hLayout->addItem(new QSpacerItem(10, 10, QSizePolicy::Expanding, QSizePolicy::Minimum));
vLayout->addLayout(hLayout);
}
window->setLayout(vLayout);
window->setStyleSheet("QLabel {border: 1px solid red;}");
return app.exec();
}
我有 QLayout
class 接受 QLabel
并将其对齐为 Qt::AlignRight
在单元格中,最初我认为它正在工作(因为布局单元格的大小是正好等于像素图的大小),我有一个与 QLabel
关联的事件,即当 mousepressEvent
发生时 QLabel
的大小增加(单元格的大小也增加,因此整个列的大小增加),那时 QLayout
中的其他 QLabel
左对齐,我希望它们右对齐或居中对齐而不是左对齐,
我的代码是,
Container::Container()
{
Layout = new QGridLayout;
Layout->setHorizontalSpacing(0);
Layout->setVerticalSpacing(10);
Layout->setMargin(10);
for(int i = 0; i < 4; ++i)
{
holes[i] = new Hole;
Layout->addWidget(ui_holes[i], i, 0, 1, Qt::AlignRight);
ui_holes[i].setPixmap("mypixmapname.png")
}
Layout->setAlignment(Qt::AlignCenter);
setLayout(Layout);
setMaximumSize(200,760);
setMinimumSize(200,760);
setSizePolicy( QSizePolicy::Fixed, QSizePolicy::Fixed);
}
void Screen::mousePressEvent(QMouseEvent *tevent)
{
if(childAt(tevent->pos()))
{
if(childAt(tevent->pos())->objectName() == "Hole")
{
hole = static_cast<Hole *>(childAt(tevent->pos()));
hole->resize(QSize(160,160));
}
}
}
void Screen::mouseReleaseEvent(QMouseEvent*)
{
if(hole)
{
ui_Hole->resetSize();
}
}
Hole 是 class 继承自 QLabel 并且我为 Hole 创建了两个新的成员函数是 resetSize 和 resize,
void Hole::resize(QSize size)
{
setSize(size);
if(!ui_HoleFlags[PIXMAP_EXISTS])
return void(0);
QPixmap *tempPixmap = ui_resourceIcon();
setPixmap(tempPixmap->scaled(size,Qt::IgnoreAspectRatio));
delete tempPixmap;
}
QPixmap* Hole::ui_resourceIcon()
{
if(!ui_HoleFlags[ICON_EXISTS])
return NULL;
QPixmap *tempPixmap = new QPixmap(*pixmap());
return tempPixmap;
}
void Hole::setSize(QSize size)
{
setMaximumSize(size);
setMinimumSize(size);
}
void Hole::resetSize()
{
if(ui_HoleFlags[PIXMAP_EXISTS])
setPixmap(*Pixmap);
setSize(ICON_SIZE);
}
提前致谢
这样,无论单个标签的大小如何,您都可以使标签始终居中对齐。
#include <QLabel>
#include <QApplication>
#include <QLayout>
#include <QMouseEvent>
class Window : public QWidget
{
public:
Window(QWidget *parent = 0) {}
virtual ~Window() {}
protected:
void mousePressEvent(QMouseEvent *event)
{
if (childAt(event->pos())) {
QLabel *label = dynamic_cast<QLabel *>(childAt(event->pos()));
if (label) {
label->setMinimumSize(QSize(50, 50));
label->setMaximumSize(QSize(50, 50));
}
}
}
};
int main(int argc, char **argv)
{
QApplication app(argc, argv);
Window *window = new Window;
window->show();
window->resize(400, 300);
QVBoxLayout *vLayout = new QVBoxLayout;
QLabel *labels[4];
for(int i = 0; i < 4; ++i) {
labels[i] = new QLabel();
labels[i]->setMaximumSize(30, 30);
labels[i]->setMinimumSize(30, 30);
QHBoxLayout *hLayout = new QHBoxLayout;
hLayout->addItem(new QSpacerItem(10, 10, QSizePolicy::Expanding, QSizePolicy::Minimum));
hLayout->addWidget(labels[i]);
hLayout->addItem(new QSpacerItem(10, 10, QSizePolicy::Expanding, QSizePolicy::Minimum));
vLayout->addLayout(hLayout);
}
window->setLayout(vLayout);
window->setStyleSheet("QLabel {border: 1px solid red;}");
return app.exec();
}