如何在Qt中将表单用作全局变量? [英] How can I use a form as a global variable in Qt?

查看:72
本文介绍了如何在Qt中将表单用作全局变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,所以我知道这可能是一个愚蠢的问题,但在Qt 5的NewForm.h头文件中,我在公共下面有以下内容:



Okay, so I know this is probably a stupid question, but in my "NewForm.h" header file in Qt 5, I have the following under "Public":

MainWindow::MainWindow *mw = new MainWindow::MainWindow();





显然我的主窗口类是MainWindow。它的头文件(在NewForm.h中导入,如下所示:





With my main window class being "MainWindow", obviously. Its header file (which is imported in "NewForm.h", looks like this:

#ifndef MAINWINDOW_H
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QPropertyAnimation>
#include <QMessageBox>
#include <QGraphicsOpacityEffect>
#include <NewForm.h>
#include <QFormLayout>
#include <QSequentialAnimationGroup>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT
    
public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

private:

    Ui::MainWindow *ui;

};

#endif // MAINWINDOW_H





然而,当我尝试编译时,我得到:





Yet, when I try to compile, I get:

error: C2653: 'MainWindow' : is not a class or namespace name
error: C2143: syntax error : missing ';' before '*'
error: C4430: missing type specifier - int assumed. Note: C++ does not support default-int
error: C2653: 'MainWindow' : is not a class or namespace name
error: C2061: syntax error : identifier 'MainWindow'
error: C4430: missing type specifier - int assumed. Note: C++ does not support default-int
error: C2864: 'NewForm::mw' : only static const integral data members can be initialized within a class
error: C1903: unable to recover from previous error(s); stopping compilation





我显然做错了什么......我来自.NET(C#)背景,并且我知道,这样的动作应该可以在C ++中实现,不是吗?我确定这是一些小问题,并再次原谅我这个愚蠢的问题,但有人可以帮我一把吗?提前致谢!非常感谢。



I'm obviously doing something wrong... I come from a .NET (C#) background, and as far as I was aware, such an action should be possible in C++, no? I'm sure it's something little, and again, forgive me for the stupid question, but could someone please give me a hand? Thanks in advance! It's much appreciated.

推荐答案

//The short answer is the line below needs to be in a .cpp file not a .h file
MainWindow::MainWindow *mw = new MainWindow::MainWindow();

//The following is longer break down of the errors.  I may have gone overboard.

//error: C2653: 'MainWindow' : is not a class or namespace name is referring to the first 'MainWindow' before the '::'
//error: C2143: syntax error : missing ';' before '*' is referring the the '*' that is after a second 'MainWindow' just after the '::'
//error: C4430: missing type specifier - int assumed. Note: C++ does not support default-int
//this is telling you the 'mw' is not understood by the complier nor is it going to help you this time, because when it did before, all kinds of virus got in the cracks.
//error: C2653: 'MainWindow' : is not a class or namespace name  
//sorry your: 

//#ifndef MAINWINDOW_H
//#ifndef MAINWINDOW_H
//#define MAINWINDOW_H
//#include <qmainwindow>

//didn't work, maybe because it is already used by the OS.

//error: C2061: syntax error : identifier 'MainWindow' well, here it got by the '=', and the 'new', but now your scope of a scope of a scope may be an issue.
//error: C4430: missing type specifier - int assumed. Note: C++ does not support default-int it didn't like the other half either, 'MainWindow()';
//error: C4430: missing type specifier - int assumed. Note: C++ does not support default-int
//see above
//error: C2864: 'NewForm::mw' : only static const integral data members can be initialized within a class  As it says, only in the .CPP file, not the .H file.
//error: C2864: 'NewForm::mw' : only static const integral data members can be initialized within a class  How did this get in here?





您确定要完全符合我们要求吗?



Are you sure you are giving us full compliance?


以下是您展示的内容。我将添加评论和更改,然后看看它有什么区别。



Here is what you have shown. I'll add comments and the changes and then see what, if any, difference that it makes.

//#ifndef MAINWINDOW_H
//#ifndef MAINWINDOW_H //Error you duplicated the above line
//#define MAINWINDOW_H
//Possible changes may help.
#ifdef MAINWINDOW_H
#undef MAINWINDOW_H
#define MAINWINDOW_H
//? What is Qt?

#include <QMainWindow>
#include <QPropertyAnimation>
#include <QMessageBox>
#include <QGraphicsOpacityEffect>
#include <NewForm.h>
#include <QFormLayout>
#include <QSequentialAnimationGroup>

namespace Ui {class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT// pretty sure you needed a ';'

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

private:

    Ui::MainWindow *ui;

};

#endif // MAINWINDOW_H





以下是我认为可能是您的问题的修改版本。





Below is my modified version of what I think may be your issues.

#if !defined(MAINWINDOW_H__F188ACDC_1956_4C5B_9A04_CBFEBD566608__INCLUDED_)
#define MAINWINDOW_H__F188ACDC_1956_4C5B_9A04_CBFEBD566608__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#ifndef __AFXWIN_H__
	#error include 'stdafx.h' before including this file for PCH
#endif

#define Q_OBJECT
#include <qmainwindow.h>
#include <QPropertyAnimation.h>
#include <QMessageBox.h>
#include <QGraphicsOpacityEffect.h>
#include <NewForm.h>
#include <QFormLayout.h>
#include <QSequentialAnimationGroup.h>

/*  This more akin to C# than C++, however it shows a proper usage of 'using' and 'namespace'
using System;
using System.Collections.Generic;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Channels;

namespace Microsoft.WCF.Documentation
{
  [ServiceContract(Namespace = "Microsoft.WCF.Documentation")]
  interface IMessagingHello
  {
    [OperationContract(
     Action = "http://GreetingMessage/Action",
     ReplyAction = "http://HelloResponseMessage/Action"
    )]
    HelloResponseMessage Hello(HelloGreetingMessage msg);
  }

  class MessagingHello : IMessagingHello
  {
    public HelloResponseMessage Hello(HelloGreetingMessage msg)
    {
      Console.WriteLine("Caller sent: " + msg.Greeting);
      HelloResponseMessage responseMsg = new HelloResponseMessage();
      responseMsg.Response = "Service received: " + msg.Greeting;
      responseMsg.ExtraValues = String.Format("Served by object {0}.", this.GetHashCode().ToString());
      Console.WriteLine("Returned response message.");
      return responseMsg;
    }
  }
}
*/
namespace Ui
{
   class CMainWindow : public QMainWindow
   {
      Q_OBJECT = NULL;

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

    private:

    Ui::CMainWindow *ui;

   }
   CMainWindow* mw;

} // This will declare the Ui namespace, presuming it is fleshed out better.
//class MainWindow; This works a forward class declaration, but is not needed here.

#endif // MAINWINDOW_H

</qmainwindow.h>


这篇关于如何在Qt中将表单用作全局变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆