所以我创建了一个DemoPromotedWidget类,它继承自QWidget。
然后我在设计器中创建了一个Widget,为所述小部件添加了一个名为DemoLabel的标签,&将小部件提升为DemoPromotedWidget类...然后我将它移动到我的工具箱中,它现在位于“Scratchpad”下。
通常,使用Qt的设计器,我可以在其对应的类文件中访问UI文件中的任何元素。但是,在DemoPromotedWidget中我不能。
无论如何我都可以在DemoPromotedWidget中访问DemoLabel,这样我就可以调用类似的东西......
ui->DemoPromotedWidget->setText("Updated Text");
这是我的UI Designer的图片,已经添加了DemoPromotedWidget。 https://i.imgur.com/QdT0eeG.png
DemoPromotedWidget.h
#ifndef DEMOPROMOTEDWIDGET_H
#define DEMOPROMOTEDWIDGET_H
#include <QWidget>
class DemoPromotedWidget : public QWidget
{
Q_OBJECT
public:
explicit DemoPromotedWidget(QWidget *parent = nullptr);
signals:
public slots:
};
#endif // DEMOPROMOTEDWIDGET_H
DemoPromotedWidget.cpp
#include "demopromotedwidget.h"
DemoPromotedWidget::DemoPromotedWidget(QWidget *parent) : QWidget(parent)
{
// How do I access DemoPromotedWidget->DemoLabel->setText()?????????????????????
}
这是我的mainwindow.ui ...
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>711</width>
<height>418</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralWidget">
<widget class="DemoPromotedWidget" name="DemoPromotedWidget" native="true">
<property name="geometry">
<rect>
<x>160</x>
<y>60</y>
<width>381</width>
<height>191</height>
</rect>
</property>
<property name="styleSheet">
<string notr="true">background-color: black;
text-align: center;</string>
</property>
<widget class="QLabel" name="DemoLabel">
<property name="geometry">
<rect>
<x>0</x>
<y>5</y>
<width>381</width>
<height>181</height>
</rect>
</property>
<property name="styleSheet">
<string notr="true">color: white;
text-align: center;</string>
</property>
<property name="text">
<string>DemoLabel</string>
</property>
<property name="alignment">
<set>Qt::AlignCenter</set>
</property>
</widget>
</widget>
</widget>
<widget class="QMenuBar" name="menuBar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>711</width>
<height>21</height>
</rect>
</property>
</widget>
<widget class="QToolBar" name="mainToolBar">
<attribute name="toolBarArea">
<enum>TopToolBarArea</enum>
</attribute>
<attribute name="toolBarBreak">
<bool>false</bool>
</attribute>
</widget>
<widget class="QStatusBar" name="statusBar"/>
</widget>
<layoutdefault spacing="6" margin="11"/>
<customwidgets>
<customwidget>
<class>DemoPromotedWidget</class>
<extends>QWidget</extends>
<header>demopromotedwidget.h</header>
<container>1</container>
</customwidget>
</customwidgets>
<resources/>
<connections/>
</ui>
答案 0 :(得分:2)
您不应该使用具有窗口小部件名称的类的名称,因此首先要做的是更改其名称:
另一方面,QLabel
内的DemoPromotedWidget
并不意味着DemoPromotedWidget
可以在构造函数中访问QLabel
及更少,因为在该方法中它是QLabel
尚未添加到主窗口,可能的解决方案是创建一个属性,您可以在其中为#ifndef DEMOPROMOTEDWIDGET_H
#define DEMOPROMOTEDWIDGET_H
#include <QLabel>
#include <QWidget>
class DemoPromotedWidget : public QWidget
{
Q_OBJECT
public:
explicit DemoPromotedWidget(QWidget *parent = nullptr);
QLabel *getLabel() const;
void setLabel(QLabel *value);
private:
QLabel *label;
};
#endif // DEMOPROMOTEDWIDGET_H
分配:
<强> demopromotedwidget.h 强>
#include "demopromotedwidget.h"
DemoPromotedWidget::DemoPromotedWidget(QWidget *parent) : QWidget(parent)
{
}
QLabel *DemoPromotedWidget::getLabel() const
{
return label;
}
void DemoPromotedWidget::setLabel(QLabel *value)
{
label = value;
if(label)
label->setText("some text");
}
<强> demopromotedwidget.cpp 强>
...
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->widget->setLabel(ui->DemoLabel);
}
...
<强> mainwindow.cpp 强>
showEvent()
另一种更简单的方法,但只有在使用Qt Designer添加小部件(QLabel)时才能使用方法findChild()
和#ifndef DEMOPROMOTEDWIDGET_H
#define DEMOPROMOTEDWIDGET_H
#include <QLabel>
#include <QWidget>
class DemoPromotedWidget : public QWidget
{
Q_OBJECT
public:
explicit DemoPromotedWidget(QWidget *parent = nullptr);
private:
QLabel *label;
protected:
void showEvent(QShowEvent *event);
};
#endif // DEMOPROMOTEDWIDGET_H
:
<强> demopromotedwidget.h 强>
#include "demopromotedwidget.h"
DemoPromotedWidget::DemoPromotedWidget(QWidget *parent) : QWidget(parent)
{
}
void DemoPromotedWidget::showEvent(QShowEvent *event)
{
label = findChild<QLabel *>("DemoLabel");
if(label)
label->setText("some text");
QWidget::showEvent(event);
}
<强> demopromotedwidget.cpp 强>
.ui
一种新方法是,您创建的表单是QWidget,并且具有.h
,.cpp
和select id, case when nulls=3 then coalesce(ethn_1, ethn_2, ethn_3, ethn_4) else 'Unknown' end variablevalue from (
select a.*, b.nulls from users a join (
select id, case when ethn_1 is null then 1 else 0 end+
case when ethn_2 is null then 1 else 0 end +
case when ethn_3 is null then 1 else 0 end +
case when ethn_4 is null then 1 else 0 end nulls
from users
group by id, ethn_1, ethn_2, ethn_3, ethn_4) b on a.id=b.id)a
:
正如我所说,创建一个与初始代码不同的新类具有.ui,然后在Qt Designer的帮助下打开它并添加标签
但它也是一个你可以像第一种方法那样推广的课程。
完整示例可在以下link
中找到