首页 > 解决方案 > 如何将 .ui 完全转换为 C++ 头文件和源文件

问题描述

我用 Qt Creator Design-Tool 创建了一些东西。通过这样做,我得到了 a .ui、 a.h和两个.cpp( mainwindow.cppand main.cpp) 文件,其中包含以下内容:

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>819</width>
    <height>634</height>
   </rect>
  </property>
  <property name="minimumSize">
   <size>
    <width>580</width>
    <height>400</height>
   </size>
  </property>
  <property name="windowTitle">
   <string>MainWindow</string>
  </property>
  <widget class="QWidget" name="centralwidget">
   <layout class="QGridLayout" name="gridLayout" rowstretch="1,4,4,0" columnstretch="2,0" rowminimumheight="1,1,10,1" columnminimumwidth="1,1">
    <property name="topMargin">
     <number>0</number>
    </property>
    <property name="bottomMargin">
     <number>0</number>
    </property>
    <property name="verticalSpacing">
     <number>6</number>
    </property>
    <item row="0" column="1">
     <widget class="QFrame" name="selectColonyFrame">
      <property name="minimumSize">
       <size>
        <width>150</width>
        <height>40</height>
       </size>
      </property>
      <property name="layoutDirection">
       <enum>Qt::RightToLeft</enum>
      </property>
      <property name="frameShape">
       <enum>QFrame::StyledPanel</enum>
      </property>
      <property name="frameShadow">
       <enum>QFrame::Raised</enum>
      </property>
      <widget class="QPushButton" name="selectColonyButton">
       <property name="geometry">
        <rect>
         <x>80</x>
         <y>0</y>
         <width>150</width>
         <height>40</height>
        </rect>
       </property>
       <property name="minimumSize">
        <size>
         <width>150</width>
         <height>40</height>
        </size>
       </property>
       <property name="maximumSize">
        <size>
         <width>150</width>
         <height>50</height>
        </size>
       </property>
       <property name="font">
        <font>
         <pointsize>11</pointsize>
         <weight>75</weight>
         <bold>true</bold>
        </font>
       </property>
       <property name="text">
        <string>Andere Kolonie ...</string>
       </property>
      </widget>
     </widget>
    </item>
    <item row="0" column="0">
     <widget class="QFrame" name="currentColonyFrame">
      <property name="sizePolicy">
       <sizepolicy hsizetype="Minimum" vsizetype="Minimum">
        <horstretch>0</horstretch>
        <verstretch>0</verstretch>
       </sizepolicy>
      </property>
      <property name="minimumSize">
       <size>
        <width>0</width>
        <height>25</height>
       </size>
      </property>
      <property name="frameShape">
       <enum>QFrame::StyledPanel</enum>
      </property>
      <property name="frameShadow">
       <enum>QFrame::Raised</enum>
      </property>
      <widget class="QLabel" name="currentColonyLabel">
       <property name="geometry">
        <rect>
         <x>10</x>
         <y>10</y>
         <width>301</width>
         <height>16</height>
        </rect>
       </property>
       <property name="minimumSize">
        <size>
         <width>220</width>
         <height>15</height>
        </size>
       </property>
       <property name="maximumSize">
        <size>
         <width>460</width>
         <height>25</height>
        </size>
       </property>
       <property name="font">
        <font>
         <pointsize>10</pointsize>
        </font>
       </property>
       <property name="text">
        <string>Aktuelle Kolonie: Lithium-Stollen 13-A</string>
       </property>
      </widget>
     </widget>
    </item>
    <item row="3" column="1">
     <widget class="QFrame" name="missionControlsFrame">
      <property name="sizePolicy">
       <sizepolicy hsizetype="Preferred" vsizetype="Preferred">
        <horstretch>8</horstretch>
        <verstretch>8</verstretch>
       </sizepolicy>
      </property>
      <property name="maximumSize">
       <size>
        <width>300</width>
        <height>180</height>
       </size>
      </property>
      <property name="frameShape">
       <enum>QFrame::WinPanel</enum>
      </property>
      <property name="frameShadow">
       <enum>QFrame::Raised</enum>
      </property>
      <layout class="QVBoxLayout" name="verticalLayout">
       <item>
        <widget class="QLabel" name="misisonControlsLabel">
         <property name="font">
          <font>
           <pointsize>11</pointsize>
          </font>
         </property>
         <property name="text">
          <string>Missionsablauf</string>
         </property>
         <property name="alignment">
          <set>Qt::AlignCenter</set>
         </property>
        </widget>
       </item>
       <item>
        <layout class="QGridLayout" name="missionControlsGridLayout">
         <property name="leftMargin">
          <number>15</number>
         </property>
         <property name="topMargin">
          <number>20</number>
         </property>
         <property name="rightMargin">
          <number>15</number>
         </property>
         <property name="bottomMargin">
          <number>0</number>
         </property>
         <property name="horizontalSpacing">
          <number>20</number>
         </property>
         <property name="verticalSpacing">
          <number>10</number>
         </property>
         <item row="0" column="0">
          <widget class="QPushButton" name="startButton">
           <property name="minimumSize">
            <size>
             <width>80</width>
             <height>30</height>
            </size>
           </property>
           <property name="maximumSize">
            <size>
             <width>130</width>
             <height>50</height>
            </size>
           </property>
           <property name="font">
            <font>
             <pointsize>12</pointsize>
            </font>
           </property>
           <property name="text">
            <string>Start</string>
           </property>
          </widget>
         </item>
         <item row="0" column="1">
          <widget class="QPushButton" name="abortButton">
           <property name="minimumSize">
            <size>
             <width>80</width>
             <height>30</height>
            </size>
           </property>
           <property name="maximumSize">
            <size>
             <width>130</width>
             <height>50</height>
            </size>
           </property>
           <property name="font">
            <font>
             <pointsize>12</pointsize>
            </font>
           </property>
           <property name="layoutDirection">
            <enum>Qt::LeftToRight</enum>
           </property>
           <property name="text">
            <string>Abbruch</string>
           </property>
           <property name="autoDefault">
            <bool>false</bool>
           </property>
           <property name="default">
            <bool>false</bool>
           </property>
           <property name="flat">
            <bool>false</bool>
           </property>
          </widget>
         </item>
         <item row="1" column="0">
          <widget class="QPushButton" name="singleStepButton">
           <property name="minimumSize">
            <size>
             <width>80</width>
             <height>30</height>
            </size>
           </property>
           <property name="maximumSize">
            <size>
             <width>130</width>
             <height>50</height>
            </size>
           </property>
           <property name="font">
            <font>
             <pointsize>12</pointsize>
            </font>
           </property>
           <property name="text">
            <string>Einzelschritt</string>
           </property>
          </widget>
         </item>
         <item row="1" column="1">
          <widget class="QPushButton" name="autoButton">
           <property name="minimumSize">
            <size>
             <width>80</width>
             <height>30</height>
            </size>
           </property>
           <property name="maximumSize">
            <size>
             <width>130</width>
             <height>50</height>
            </size>
           </property>
           <property name="font">
            <font>
             <pointsize>12</pointsize>
            </font>
           </property>
           <property name="layoutDirection">
            <enum>Qt::LeftToRight</enum>
           </property>
           <property name="text">
            <string>Automatik</string>
           </property>
           <property name="autoDefault">
            <bool>false</bool>
           </property>
           <property name="default">
            <bool>false</bool>
           </property>
           <property name="flat">
            <bool>false</bool>
           </property>
          </widget>
         </item>
         <item row="2" column="1">
          <widget class="QPushButton" name="pauseButton">
           <property name="minimumSize">
            <size>
             <width>80</width>
             <height>30</height>
            </size>
           </property>
           <property name="maximumSize">
            <size>
             <width>130</width>
             <height>50</height>
            </size>
           </property>
           <property name="font">
            <font>
             <pointsize>12</pointsize>
            </font>
           </property>
           <property name="layoutDirection">
            <enum>Qt::LeftToRight</enum>
           </property>
           <property name="text">
            <string>Anhalten</string>
           </property>
           <property name="autoDefault">
            <bool>false</bool>
           </property>
           <property name="default">
            <bool>false</bool>
           </property>
           <property name="flat">
            <bool>false</bool>
           </property>
          </widget>
         </item>
        </layout>
       </item>
      </layout>
     </widget>
    </item>
    <item row="1" column="1" rowspan="2">
     <widget class="QFrame" name="botControlsFrame">
      <property name="sizePolicy">
       <sizepolicy hsizetype="Preferred" vsizetype="Preferred">
        <horstretch>4</horstretch>
        <verstretch>4</verstretch>
       </sizepolicy>
      </property>
      <property name="minimumSize">
       <size>
        <width>160</width>
        <height>120</height>
       </size>
      </property>
      <property name="maximumSize">
       <size>
        <width>320</width>
        <height>240</height>
       </size>
      </property>
      <property name="frameShape">
       <enum>QFrame::WinPanel</enum>
      </property>
      <property name="frameShadow">
       <enum>QFrame::Raised</enum>
      </property>
      <layout class="QGridLayout" name="gridLayout_2" rowstretch="0,2,1,1">
       <item row="1" column="0" rowspan="3">
        <layout class="QGridLayout" name="botControlsGridLayout" rowstretch="0,0,0" columnstretch="0,0,0">
         <property name="sizeConstraint">
          <enum>QLayout::SetMinimumSize</enum>
         </property>
         <item row="2" column="1">
          <widget class="QPushButton" name="downButton">
           <property name="minimumSize">
            <size>
             <width>30</width>
             <height>30</height>
            </size>
           </property>
           <property name="maximumSize">
            <size>
             <width>50</width>
             <height>50</height>
            </size>
           </property>
           <property name="layoutDirection">
            <enum>Qt::LeftToRight</enum>
           </property>
           <property name="text">
            <string/>
           </property>
           <property name="icon">
            <iconset resource="grafics.qrc">
             <normaloff>:/grafics/grafics/pfeile/pfeil_unten.tga</normaloff>:/grafics/grafics/pfeile/pfeil_unten.tga</iconset>
           </property>
           <property name="iconSize">
            <size>
             <width>50</width>
             <height>50</height>
            </size>
           </property>
          </widget>
         </item>
         <item row="0" column="1">
          <widget class="QPushButton" name="upButton">
           <property name="minimumSize">
            <size>
             <width>30</width>
             <height>30</height>
            </size>
           </property>
           <property name="maximumSize">
            <size>
             <width>50</width>
             <height>50</height>
            </size>
           </property>
           <property name="sizeIncrement">
            <size>
             <width>0</width>
             <height>0</height>
            </size>
           </property>
           <property name="text">
            <string/>
           </property>
           <property name="icon">
            <iconset resource="grafics.qrc">
             <normaloff>:/grafics/grafics/pfeile/pfeil_oben.tga</normaloff>:/grafics/grafics/pfeile/pfeil_oben.tga</iconset>
           </property>
           <property name="iconSize">
            <size>
             <width>50</width>
             <height>50</height>
            </size>
           </property>
          </widget>
         </item>
         <item row="1" column="1">
          <widget class="QPushButton" name="waitButton">
           <property name="minimumSize">
            <size>
             <width>30</width>
             <height>30</height>
            </size>
           </property>
           <property name="maximumSize">
            <size>
             <width>50</width>
             <height>50</height>
            </size>
           </property>
           <property name="font">
            <font>
             <family>MS Shell Dlg 2</family>
             <pointsize>15</pointsize>
            </font>
           </property>
           <property name="contextMenuPolicy">
            <enum>Qt::DefaultContextMenu</enum>
           </property>
           <property name="text">
            <string>W</string>
           </property>
           <property name="icon">
            <iconset theme="&amp;#9664">
             <normaloff>.</normaloff>.</iconset>
           </property>
          </widget>
         </item>
         <item row="1" column="0">
          <widget class="QPushButton" name="leftButton">
           <property name="minimumSize">
            <size>
             <width>30</width>
             <height>30</height>
            </size>
           </property>
           <property name="maximumSize">
            <size>
             <width>50</width>
             <height>50</height>
            </size>
           </property>
           <property name="text">
            <string/>
           </property>
           <property name="icon">
            <iconset resource="grafics.qrc">
             <normaloff>:/grafics/grafics/pfeile/pfeil_links.tga</normaloff>:/grafics/grafics/pfeile/pfeil_links.tga</iconset>
           </property>
           <property name="iconSize">
            <size>
             <width>50</width>
             <height>50</height>
            </size>
           </property>
          </widget>
         </item>
         <item row="0" column="2">
          <widget class="QPushButton" name="deleteButton">
           <property name="minimumSize">
            <size>
             <width>30</width>
             <height>30</height>
            </size>
           </property>
           <property name="maximumSize">
            <size>
             <width>50</width>
             <height>50</height>
            </size>
           </property>
           <property name="font">
            <font>
             <pointsize>15</pointsize>
            </font>
           </property>
           <property name="text">
            <string>&lt;</string>
           </property>
          </widget>
         </item>
         <item row="1" column="2">
          <widget class="QPushButton" name="rightButton">
           <property name="minimumSize">
            <size>
             <width>30</width>
             <height>30</height>
            </size>
           </property>
           <property name="maximumSize">
            <size>
             <width>50</width>
             <height>50</height>
            </size>
           </property>
           <property name="sizeIncrement">
            <size>
             <width>0</width>
             <height>0</height>
            </size>
           </property>
           <property name="text">
            <string/>
           </property>
           <property name="icon">
            <iconset resource="grafics.qrc">
             <normaloff>:/grafics/grafics/pfeile/pfeil_rechts.tga</normaloff>:/grafics/grafics/pfeile/pfeil_rechts.tga</iconset>
           </property>
           <property name="iconSize">
            <size>
             <width>50</width>
             <height>50</height>
            </size>
           </property>
          </widget>
         </item>
        </layout>
       </item>
       <item row="0" column="0" colspan="2">
        <widget class="QLabel" name="botControlsLabel">
         <property name="font">
          <font>
           <pointsize>11</pointsize>
          </font>
         </property>
         <property name="text">
          <string>Programmieren</string>
         </property>
         <property name="alignment">
          <set>Qt::AlignCenter</set>
         </property>
        </widget>
       </item>
       <item row="1" column="1">
        <widget class="QComboBox" name="repeatDropdown">
         <property name="minimumSize">
          <size>
           <width>80</width>
           <height>30</height>
          </size>
         </property>
         <property name="maximumSize">
          <size>
           <width>130</width>
           <height>50</height>
          </size>
         </property>
         <property name="font">
          <font>
           <pointsize>15</pointsize>
           <weight>75</weight>
           <italic>false</italic>
           <bold>true</bold>
          </font>
         </property>
         <property name="focusPolicy">
          <enum>Qt::WheelFocus</enum>
         </property>
         <property name="layoutDirection">
          <enum>Qt::LeftToRight</enum>
         </property>
         <property name="currentText">
          <string>1</string>
         </property>
         <property name="insertPolicy">
          <enum>QComboBox::NoInsert</enum>
         </property>
         <property name="frame">
          <bool>true</bool>
         </property>
         <item>
          <property name="text">
           <string>1</string>
          </property>
         </item>
         <item>
          <property name="text">
           <string>2</string>
          </property>
         </item>
         <item>
          <property name="text">
           <string>3</string>
          </property>
         </item>
         <item>
          <property name="text">
           <string>4</string>
          </property>
         </item>
         <item>
          <property name="text">
           <string>5</string>
          </property>
         </item>
        </widget>
       </item>
       <item row="2" column="1">
        <widget class="QFrame" name="frame_6">
         <property name="frameShape">
          <enum>QFrame::StyledPanel</enum>
         </property>
         <property name="frameShadow">
          <enum>QFrame::Raised</enum>
         </property>
        </widget>
       </item>
       <item row="3" column="1">
        <widget class="QFrame" name="botCommandFrame">
         <property name="minimumSize">
          <size>
           <width>100</width>
           <height>80</height>
          </size>
         </property>
         <property name="maximumSize">
          <size>
           <width>130</width>
           <height>16777215</height>
          </size>
         </property>
         <property name="frameShape">
          <enum>QFrame::StyledPanel</enum>
         </property>
         <property name="frameShadow">
          <enum>QFrame::Raised</enum>
         </property>
         <widget class="QTextEdit" name="botCommandTextEdit">
          <property name="geometry">
           <rect>
            <x>0</x>
            <y>16</y>
            <width>108</width>
            <height>50</height>
           </rect>
          </property>
          <property name="sizePolicy">
           <sizepolicy hsizetype="Expanding" vsizetype="Expanding">
            <horstretch>130</horstretch>
            <verstretch>50</verstretch>
           </sizepolicy>
          </property>
          <property name="minimumSize">
           <size>
            <width>80</width>
            <height>30</height>
           </size>
          </property>
          <property name="maximumSize">
           <size>
            <width>130</width>
            <height>50</height>
           </size>
          </property>
          <property name="font">
           <font>
            <pointsize>15</pointsize>
           </font>
          </property>
          <property name="verticalScrollBarPolicy">
           <enum>Qt::ScrollBarAlwaysOff</enum>
          </property>
          <property name="horizontalScrollBarPolicy">
           <enum>Qt::ScrollBarAlwaysOn</enum>
          </property>
          <property name="lineWrapMode">
           <enum>QTextEdit::NoWrap</enum>
          </property>
          <property name="readOnly">
           <bool>true</bool>
          </property>
          <property name="placeholderText">
           <string>Textfeld</string>
          </property>
         </widget>
        </widget>
       </item>
      </layout>
     </widget>
    </item>
    <item row="1" column="0" rowspan="3">
     <widget class="QFrame" name="mapFrame">
      <property name="sizePolicy">
       <sizepolicy hsizetype="MinimumExpanding" vsizetype="MinimumExpanding">
        <horstretch>0</horstretch>
        <verstretch>0</verstretch>
       </sizepolicy>
      </property>
      <property name="minimumSize">
       <size>
        <width>120</width>
        <height>120</height>
       </size>
      </property>
      <property name="frameShape">
       <enum>QFrame::StyledPanel</enum>
      </property>
      <property name="frameShadow">
       <enum>QFrame::Raised</enum>
      </property>
      <layout class="QGridLayout" name="gridLayout_6">
       <property name="spacing">
        <number>0</number>
       </property>
       <item row="0" column="0">
        <widget class="QLabel" name="label">
         <property name="sizePolicy">
          <sizepolicy hsizetype="MinimumExpanding" vsizetype="MinimumExpanding">
           <horstretch>0</horstretch>
           <verstretch>0</verstretch>
          </sizepolicy>
         </property>
         <property name="minimumSize">
          <size>
           <width>0</width>
           <height>0</height>
          </size>
         </property>
         <property name="frameShape">
          <enum>QFrame::Box</enum>
         </property>
         <property name="text">
          <string>Umgebungskarte</string>
         </property>
         <property name="alignment">
          <set>Qt::AlignCenter</set>
         </property>
        </widget>
       </item>
       <item row="0" column="1">
        <widget class="QScrollBar" name="verticalScrollBar">
         <property name="orientation">
          <enum>Qt::Vertical</enum>
         </property>
        </widget>
       </item>
       <item row="1" column="0">
        <widget class="QScrollBar" name="horizontalScrollBar">
         <property name="sizePolicy">
          <sizepolicy hsizetype="MinimumExpanding" vsizetype="Fixed">
           <horstretch>0</horstretch>
           <verstretch>0</verstretch>
          </sizepolicy>
         </property>
         <property name="orientation">
          <enum>Qt::Horizontal</enum>
         </property>
        </widget>
       </item>
      </layout>
     </widget>
    </item>
   </layout>
  </widget>
  <widget class="QMenuBar" name="menubar">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
     <width>819</width>
     <height>20</height>
    </rect>
   </property>
  </widget>
  <widget class="QStatusBar" name="statusbar"/>
 </widget>
 <resources>
  <include location="grafics.qrc"/>
 </resources>
 <connections/>
</ui>

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

private slots:
    void on_upButton_clicked();

    void on_rightButton_clicked();

    void on_downButton_clicked();

    void on_leftButton_clicked();

    void on_waitButton_clicked();

    void on_deleteButton_clicked();

    void on_selectColonyButton_clicked();

    void on_startButton_clicked();

    void on_abortButton_clicked();

    void on_singleStepButton_clicked();

    void on_autoButton_clicked();

    void on_pauseButton_clicked();

private:
    Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H

mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"

#include "QMessageBox"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

MainWindow::~MainWindow()
{
    delete ui;
}


void MainWindow::on_upButton_clicked()
{
    QMessageBox::information(this, "button", "up");
}

void MainWindow::on_rightButton_clicked()
{
    QMessageBox::information(this, "button", "right");
}

void MainWindow::on_downButton_clicked()
{
    QMessageBox::information(this, "button", "down");
}

void MainWindow::on_leftButton_clicked()
{
    QMessageBox::information(this, "button", "left");
}

void MainWindow::on_waitButton_clicked()
{
    QMessageBox::information(this, "button", "wait");
}

void MainWindow::on_deleteButton_clicked()
{
    QMessageBox::information(this, "button", "delete");
}

void MainWindow::on_selectColonyButton_clicked()
{
    QMessageBox::information(this, "button", ui->selectColonyButton->text());
}

void MainWindow::on_startButton_clicked()
{
    QMessageBox::information(this, "button", ui->startButton->text());
}

void MainWindow::on_abortButton_clicked()
{
    QMessageBox::information(this, "button", ui->abortButton->text());
}

void MainWindow::on_singleStepButton_clicked()
{
    QMessageBox::information(this, "button", ui->singleStepButton->text());
}

void MainWindow::on_autoButton_clicked()
{
    QMessageBox::information(this, "button", ui->autoButton->text());
}

void MainWindow::on_pauseButton_clicked()
{
    QMessageBox::information(this, "button", ui->pauseButton->text());
}

main.cpp

#include "mainwindow.h"

#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    MainWindow w;
    w.show();
    return app.exec();
}

现在我想要的是一个.cpp文件,其中不仅包含我手动创建的信号和插槽,还包含所有内容。我使用设计器添加的每个布局、每个标签、每个按钮。据我所知,有一种方法可以实现这一目标:qmakeuic

现在的问题是:我如何实际使用其中之一?

我尝试uic通过命令提示符打开所有这些文件所在的文件夹并执行此操作:(uic -o mainwindow.h mainwindow.ui 谷歌搜索了一下,发现)

但我得到的错误是这样的:“uic”命令写错或找不到。(试图用我的语言翻译)

关于如何解决问题的任何想法或关于如何实现我需要的任何不同想法?

标签: c++qtqmakeuic

解决方案


使用 qmake,这里是所有基本解释的链接:

https://doc.qt.io/qt-5/qmake-tutorial.html

对于 .ui 文件,您可以像这样将它们添加到 .pro 文件中:

FORMS += foo.ui

推荐阅读