【发布时间】:2014-08-04 20:52:29
【问题描述】:
我继承了 QWidget 来创建一个名为(比如说..)TaskBox 的类。
我已将 QGridLayout 应用到我的 TaskBox。
布局由几个 QLabels 组成。
我通过设置样式表更改了任务框的背景颜色。
现在看起来像这样:
这就是我想要的,我很满意。
问题是,我想将 Q_OBJECT 宏添加到 TaskBox 类。 (因为我需要用到信号和槽)
添加Q_OBJECT宏后,我的TaskBox对象变成这样:
看起来样式表在 QGridLayout 中被分解为单元格。
这是我的 TaskBox 类:
class TaskBox : public QWidget{
Q_OBJECT
public:
QLabel * title;
QLabel * description;
QGridLayout * layout;
TaskBox(){
layout = new QGridLayout();
setRandomColor(); //Function is available below
title = new QLabel("Something");
title->setStyleSheet("color:white;");
description = new QLabel("Something again");
description->setStyleSheet("color:white;");
layout->addWidget(title, 0,0);
layout->addWidget(description,1,0);
layout->setColumnStretch(0,2);
layout->setColumnStretch(1,1);
setLayout(layout);
}
void setRandomColor(){
setStyleSheet("border-radius: 5px;background-color:rgb(" + QString::number((rand() % 255)) + "," + QString::number((rand() % 255)) + "," + QString::number((rand() % 255)) + ");");
}
};
我不明白发生了什么。
感谢您的任何帮助!
【问题讨论】:
标签: qt signals-slots moc qlayout