【问题标题】:Qt signals and slots in xmlxml中的Qt信号和槽
【发布时间】:2013-07-19 23:22:55
【问题描述】:

我已经用 Qt 玩了几个月了。我通过编码和重新创建各种版本的代码来自学; QML、XML、C++、Gui。

这种方法给了我很多洞察力。 但是我卡住了,下面的代码主要是在xml中完成。但我无法让我的信号和插槽工作。一切对我来说都很好,我想我错过了一些东西。

    <ui version="4.0">
    <class>enTry1</class>
    <widget class="QMainWindow" name="enTry1">
        <property name="geometry" >
            <rect>
                <x>0</x>
                <y>0</y>
                <width>500</width>
                <height>200</height>
            </rect>
        </property>
        <property name="windowTitle">
            <string>All xml signals and slots example</string>
        </property>
        <widget class="QWidget" name="centralWidget">
            <widget class="QPushButton" name="pushButton">
                <property name="geometry">
                    <rect>
                        <x>70</x>
                        <y>75</y>
                        <width>75</width>
                        <height>23</height>
                    </rect>
                </property>
                <property name="text">
                    <string>Pushbutton</string>
                </property>
            </widget>
            <widget class="QPushButton" name="pushButton_2">
                <property name="geometry">
                    <rect>
                        <x>70</x>
                        <y>125</y>
                        <width>75</width>
                        <height>23</height>
                    </rect>
                </property>
                <property name="text">
                    <string>Pushbutton_2</string>
                </property>
            </widget>
            <widget class="QLabel" name="label">
                <property name="geometry">
                    <rect>
                        <x>80</x>
                        <y>40</y>
                        <width>46</width>
                        <height>13</height>
                    </rect>
                </property>
                <property name="text">
                    <string>TextLabel</string>
                </property>
            </widget>
        </widget>

        <widget class="QMenuBar" name="menuBar">
            <property name="geometry">
                <rect>
                    <x>0</x>
                    <y>0</y>
                    <width>400</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" />
    <resources/>
    <connections>
        <connection>
            <sender>pushButton</sender>
            <signal>clicked()</signal>
            <receiver>enTry1</receiver>
            <slot>button1pressed()</slot>
            <hints>
                <hint type="sourcelabel">
                    <x>113</x>
                    <y>138</y>
                </hint>
                <hint type="destinationlabel">
                    <x>207</x>
                    <y>136</y>
                </hint>
            </hints>
        </connection>
        <connection>
            <sender>pushButton_2</sender>
            <signal>clicked()</signal>
            <receiver>enTry1</receiver>
            <slot>button2pressed()</slot>
            <hints>
                <hint type="sourcelabel">
                    <x>127</x>
                    <y>199</y>
                </hint>
                <hint type="destinationlabel">
                    <x>206</x>
                    <y>183</y>
                </hint>
            </hints>
        </connection>
    </connections>
    <slots>
        <slot>button1pressed()</slot>
        <slot>button2pressed()</slot>
    </slots>

</ui>

为了提供更多信息,当按下按钮时,代码什么也不运行。错误说;

QObject::connect: No such slot QLabel::button1pressed() in ./ui_entry1.h:69
QObject::connect: (sender name: ‘pushButton’)
QObject::connect: (receiver name: ‘label’)
QObject::connect: No such slot enTry1::button2pressed() in ./ui_entry1.h:70
QObject::connect: (sender name: ‘pushButton_2’)
QObject::connect: (receiver name: ‘enTry1’)

有什么建议……??

【问题讨论】:

    标签: xml qt signals slots


    【解决方案1】:

    http://qt-project.org/doc/qt-5.0/qtdesigner/designer-using-a-ui-file.html

    http://qt-project.org/doc/qt-4.8/examples-designer.html

    Ui 文件也称为:

    Qt 设计器表单

    您通常查看的 XML 是由 Qt Designer 生成的。我会遵循使用 Designer 而不是编辑生成的文件的教程。

    我没有看到 xml 的示例或在 qt 文档中编辑 xml 的含义。

    说了这么多,找出当前问题的最佳方法是打开 Qt Designer,在 QMainWindow 中创建按钮和标签以及连接,然后在生成 ui 文件后,将其与一个你尝试手写的地方。

    希望对您有所帮助。

    【讨论】:

    • 我会试试的,谢谢你的回复。我很确定我确实这样做了,因为我知道 ui 确实会自动生成代码。我想当时这就是它如此令人沮丧的原因。从那以后,我把它作为一个未解决的问题搁置了。很抱歉回复晚了,非常感谢您的帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-23
    • 1970-01-01
    • 2017-08-01
    • 2013-10-08
    • 1970-01-01
    相关资源
    最近更新 更多