【问题标题】:Qt Creator: Design changes not showing when runQt Creator:运行时未显示设计更改
【发布时间】:2012-03-17 02:09:27
【问题描述】:

我对文件夹中的“ma​​inwindow.ui”文件进行了一些更改:“表单”。 我添加了一个名为“文件”的新标题,只有两个简单的选项,“添加分形”和“退出”

你还可以看到两者都在mainwindow.ui文件中:

但是当我运行项目时,窗口没有按我的意愿显示。选项卡未显示。

我已经建造、重建和制造,但仍然有这个奇怪的东西。 谁能帮帮我?

ma​​inwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

namespace Ui {
    class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

private:
    Ui::MainWindow *ui;
    void createActions();
    void createMenus();
    // Widgets i layouts
    QWidget *centralWidget;
    // Accions associades als menus
    QAction *exitAct;
    // Menus principals
    QMenu *fileMenu;
};

#endif // MAINWINDOW_H

ma​​inwindow.cpp

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

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    centralWidget = new QWidget;
    setCentralWidget(centralWidget); createActions();
    createMenus();
    setWindowTitle(tr("Practica 1: GiVD 2011-2012"));
    resize(640, 480);
}

void MainWindow::createActions() {
    exitAct = new QAction(tr("&Exit"), this);
    exitAct->setShortcut(tr("Ctrl+Q"));
    exitAct->setStatusTip(tr("Sortir del programa"));
    connect(exitAct, SIGNAL(triggered()), qApp, SLOT(closeAllWindows()));
}

void MainWindow::createMenus()
{
    fileMenu = menuBar()->addMenu(tr("&File"));
    fileMenu->addSeparator();
    // Per a cada nou submenú has de fer un addAction
    fileMenu->addAction(exitAct);
}

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

ma​​in.cpp

#include <QtGui/QApplication>
#include "mainwindow.h"

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();

    return a.exec();
}

ma​​inwindow.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>400</width>
    <height>300</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>MainWindow</string>
  </property>
  <widget class="QWidget" name="centralWidget"/>
  <widget class="QMenuBar" name="menuBar">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
     <width>400</width>
     <height>22</height>
    </rect>
   </property>
   <widget class="QMenu" name="menuFile">
    <property name="title">
     <string>File</string>
    </property>
    <addaction name="actionAdd_Fractal"/>
    <addaction name="actionExit"/>
   </widget>
   <addaction name="menuFile"/>
  </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"/>
  <action name="actionAdd_Fractal">
   <property name="text">
    <string>Add Fractal</string>
   </property>
  </action>
  <action name="actionExit">
   <property name="text">
    <string>Exit</string>
   </property>
  </action>
 </widget>
 <layoutdefault spacing="6" margin="11"/>
 <resources/>
 <connections/>
</ui>

【问题讨论】:

  • 我个人不使用 Qt Creator,但您确定 UIC 会在您的 .ui 文件上运行吗?并且生成的头文件会更新吗?
  • 我怎么知道?对不起,巴特,但我现在使用的是 Qt Creator 和 C++。至少,在 .pro 文件中我有 'FORMS += mainwindow.ui'
  • 据我了解(但其他人可能会纠正我)将 .ui 添加到 Forms 应该确保 UIC 被调用。这应该会生成一个具有相似名称的 .h 文件,其中包含用于设置 GUI 的 C++ 代码。看看是否确实生成了这样的文件并且是您项目的一部分。
  • 向我们展示 MainWindow 类定义(在 .h 文件中)和 MainWindow 构造函数(在 .cpp 文件中)。
  • 愚蠢的问题:您检查过,您的菜单没有出现在屏幕顶部的菜单栏中吗?

标签: qt


【解决方案1】:

我遇到了同样的问题,直到我意识到 Mac OS 上的菜单出现在顶部而不是应用程序窗口中。

看看你是不是这样。

谢谢,

【讨论】:

    猜你喜欢
    • 2023-03-15
    • 1970-01-01
    • 2020-01-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-17
    相关资源
    最近更新 更多