[英]adding a custom widget in Layout at execution
我在 Ubuntu 18.04 下使用最后一个 QtCreator 和最后一个 Qt5 框架
我正在尝试设置垂直布局,顶部是水平嵌套布局,在定制小部件下方(目前基于时钟示例)
我设置了一个新的小部件项目,但由于我还不明白如何从 C++ 代码访问嵌套布局,我删除了自动创建的主窗体并在执行时创建了布局。
如果我使用我的自定义小部件作为窗口它可以工作如果我使用一个窗口并添加我的自定义小部件它可以工作但是如果我添加一个布局,将我的自定义小部件添加到这个布局并在窗口上调用 setLayout 它会消失......
我尝试了几乎所有订单:首先或在添加我的小部件之后设置布局。 我尝试在我的小部件上调用 show() 之前或之后或在将其添加到布局之后我尝试首先添加嵌套层或最后没有任何更改我已经阅读了几次关于布局和嵌套的示例和手册我可以看到我的嵌套布局但不是我的小部件
这是我的主要内容:
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QWidget w;
w.setWindowTitle("WOUND : Windings Organiser with Usefull and Neat Drawings");
QVBoxLayout* hl=new QVBoxLayout;// top vertical layout
QHBoxLayout* slotqueryLayout=new QHBoxLayout; // nested first horizontal layout
QLabel *slotqueryLabel = new QLabel("Slot number:");
QLineEdit *slotqueryEdit = new QLineEdit("48");
slotqueryLayout->addWidget(slotqueryLabel);
slotqueryLayout->addWidget(slotqueryEdit);
hl->addLayout(slotqueryLayout);
WindingViewer* wv=new WindingViewer(&w); // my widget is just a simple canvas to draw things
hl->addWidget(wv);
wv->show(); // dont know if it's needed but if I remove it it dont change anything / tried to do it before or after adding to layout
w.setLayout(hl); // if called before adding content it dont change
w.show();
return a.exec();
}
在这里你可以找到我的自定义小部件:
class WindingViewer : public QWidget
{
Q_OBJECT
public:
explicit WindingViewer(QWidget *parent = nullptr);
protected:
void paintEvent(QPaintEvent *event) override;
signals:
public :
int SlotNumber;
public slots:
};
和
WindingViewer::WindingViewer(QWidget *parent) : QWidget(parent)
{
SlotNumber=3;
resize(200, 200);
}
void WindingViewer::paintEvent(QPaintEvent *)
{
int side = qMin(width(), height());
QColor SlotColor(127, 127, 127);
QPainter painter(this);
static const QPoint slotpolygonext[4] = {
QPoint(-2,85),
QPoint(-3,95),
QPoint(3, 95),
QPoint(2, 85)
};
static const QPoint slotpolygonint[5] = {
QPoint(-1,75),
QPoint(-2,85),
QPoint(2, 85),
QPoint(1, 75),
QPoint(-1,75),
};
painter.setRenderHint(QPainter::Antialiasing);
painter.translate(width() / 2, height() / 2);
painter.scale(side / 200.0, side / 200.0);
painter.setPen(SlotColor);
for (int i = 0; i < SlotNumber; ++i) {
painter.drawPolyline(slotpolygonext,4);
painter.drawPolyline(slotpolygonint,5);
painter.rotate(360.0/SlotNumber);
}
}
我希望这个问题足够清楚。 在发布之前,我已经在这里和互联网上搜索了答案。 我发现的东西很少,但没有什么完全相关的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.