Zugriffsproblem zwischen 2 Klassen in Qt4 (MS VS C2227 Error)

MisterHabakus

Grünschnabel
Hallo zusammen

Ich habe mich an die GUI Programmierung mit Qt4 in C++ gewagt. Als System verwende ich Windows 7, Visual Studio 2008 und das Qt Addin (.ui Dateien können bequemer erstellt und dann direkt in der IDE verwendet werden). Ich habe ein kleines Testprogramm geschrieben, welches folgendes machen soll:


  • Mainwindow (Class IDE) erstellen
  • Preferenceswindow (Class Preferences) per "Singleton" erstellen
  • Preferencewindow anzeigen, wenn der Button dafür angeklickt wurde
  • Bei Klicken des Button ButtonSave soll auf der Textfläche des Mainwindow ein Text ausgegeben werden (Class Preferences ruft eine Methode in IDE auf)


Damit ich die ganzen GUIs nicht von Hand erstellen muss, verwende ich dafür den Qt Designer und binde diese zur Compiletime ein.

Nun zu meinem Problem. Immer wenn ich die Methode setNewText mit dem Parameter QString (text) aufrufe um im Mainwindow das Textfeld plainTextEdit zu setzen, bekomme ich einen Compilererror C2227 (MS Link wäre hier http://msdn.microsoft.com/en-US/library/0z8wd049(v=vs.80) ). Nur werde ich daraus nicht ganz schlau, denn QString wird ja als Charakterpointer mitgegeben und plainTextEdit ist der Mainclass bekannt.

@Edit: Error hinzugefügt und Post ausgebessert

Hier mein Code:

ide.cpp
C++:
#include <QObject>
#include <QString>
#include <QTextStream>

#include "ide.h"

IDE::IDE(QWidget *parent, Qt::WFlags flags) : QMainWindow(parent, flags)
{
	setupUi(this);
	connect(actionPreferences, SIGNAL(triggered()), this, SLOT(Preferences()));
}

IDE::~IDE()
{

}

void IDE::Preferences()
{
	GuiPreferences.show();
}

void IDE::setNewText(QString text)
{
	plainTextEdit->setPlainText(text); // Hier kommt es zum Fehler
}

ide.h
C++:
#ifndef IDE_H
#define IDE_H

#include <QtGui/QMainWindow>
#include "ui_ide.h"
#include "ui_preferences.h"
#include "preferences.h"

class IDE : public QMainWindow, public Ui::IDEClass
{
	Q_OBJECT

public:
	IDE(QWidget *parent = 0, Qt::WFlags flags = 0);
	~IDE();
	Preferences GuiPreferences;
	static void setNewText(QString text);
private slots:
	void Preferences();
private:
	Ui::IDEClass ui;
public:
};

#endif // IDE_H

preferences.cpp
C++:
#include "ide.h"
#include "preferences.h"

#include <QWidget>
#include <QCheckBox>

Preferences::Preferences(QWidget *parent)
{
	show_status = false;
	setupUi(this);
	QObject::connect(ButtonClose, SIGNAL(clicked()), this, SLOT(Close()));
	QObject::connect(ButtonSave, SIGNAL(clicked()), this, SLOT(startSend()));
}

void Preferences::show()
{
	if(!show_status)
	{
		QWidget::show();
		show_status = true;
	}
}

void Preferences::Close()
{
	show_status = false;
	close();
}

void Preferences::startSend()
{
	QString text = "Hallo 123";
	IDE::setNewText(text);
}

preferences.h
C++:
#ifndef PREFERENCES_H
#define PREFERENCES_H

#include <QWidget>
#include "ui_preferences.h"

class Preferences : public QWidget, private Ui::Preferences
{
Q_OBJECT

public:
	Preferences(QWidget *parent = 0);
	void show();
private slots:
	void Close();
	void startSend();
private:
	bool show_status;
};

#endif

Error:
Code:
------ Build started: Project: IDE, Configuration: Release Win32 ------
Compiling...
ide.cpp
.\ide.cpp(26) : error C2227: left of '->setPlainText' must point to class/struct/union/generic type
Build log was saved at "file://C:\...\IDE\Release\BuildLog.htm"
IDE - 1 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Mit freundlichen Grüssen
Habakus
 
Zuletzt bearbeitet:
Hallo

Man jetzt habe ich doch wirklich die Fehlermeldung vergessen (Peinlichst). Da wäre sie noch:

Code:
------ Build started: Project: IDE, Configuration: Release Win32 ------
Compiling...
ide.cpp
.\ide.cpp(26) : error C2227: left of '->setPlainText' must point to class/struct/union/generic type
Build log was saved at "file://C:\...\IDE\Release\BuildLog.htm"
IDE - 1 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Grüsse Habakus
 
Hallo

Wie bereits oben schnell angedeutet sieht das Ganze so aus:

*.ui = Das ist die Qt Designer Datei, sie enthält das Window/Widget etc. In unserem Falle haben wir eine ide.ui und eine preferences.ui

Daraus generiert MOC (Art Precompiler) dann eine Headerfile und eine .cpp Datei (Diese wird aber jedesmal aufs Neue generiert).

Nun kann man die generierten Headerfiles includieren, in unserem Falle ui_ide.h und ui_preferences.h je in ide.h und preferences.h

Hier der Ausschnitt aus der ui_ide.h (Siehe QPlainTextEdit *plainTextEdit; )

C++:
/********************************************************************************
** Form generated from reading UI file 'ide.ui'
**
** Created: Mon Jun 11 10:58:16 2012
**      by: Qt User Interface Compiler version 4.8.0
**
** WARNING! All changes made in this file will be lost when recompiling UI file!
********************************************************************************/

#ifndef UI_IDE_H
#define UI_IDE_H

#include <QtCore/QVariant>
#include <QtGui/QAction>
#include <QtGui/QApplication>
#include <QtGui/QButtonGroup>
#include <QtGui/QGridLayout>
#include <QtGui/QHeaderView>
#include <QtGui/QMainWindow>
#include <QtGui/QMenu>
#include <QtGui/QMenuBar>
#include <QtGui/QPlainTextEdit>
#include <QtGui/QStatusBar>
#include <QtGui/QToolBar>
#include <QtGui/QTreeView>
#include <QtGui/QWidget>

QT_BEGIN_NAMESPACE

class Ui_IDEClass
{
public:
    QAction *actionNew_Project;
    QAction *actionLoad_Project;
    QAction *actionNew_File;
    QAction *actionDelete_File;
    QAction *actionSave;
    QAction *actionExit;
    QAction *actionSearch;
    QAction *actionSearch_and_Replace;
    QAction *actionPreferences;
    QAction *actionVersion;
    QAction *actionAbout;
    QWidget *centralWidget;
    QGridLayout *gridLayout_2;
    QGridLayout *gridLayout;
    QTreeView *treeView;
    QPlainTextEdit *plainTextEdit;
    QMenuBar *menuBar;
    QMenu *menuFile;
    QMenu *menuEdit;
    QMenu *menuTools;
    QMenu *menuHelp;
    QToolBar *mainToolBar;
    QStatusBar *statusBar;

    void setupUi(QMainWindow *IDEClass)
    {
        if (IDEClass->objectName().isEmpty())
            IDEClass->setObjectName(QString::fromUtf8("IDEClass"));
        IDEClass->resize(600, 400);
        actionNew_Project = new QAction(IDEClass);
        actionNew_Project->setObjectName(QString::fromUtf8("actionNew_Project"));
        actionLoad_Project = new QAction(IDEClass);
        actionLoad_Project->setObjectName(QString::fromUtf8("actionLoad_Project"));
        actionNew_File = new QAction(IDEClass);
        actionNew_File->setObjectName(QString::fromUtf8("actionNew_File"));
        actionDelete_File = new QAction(IDEClass);
        actionDelete_File->setObjectName(QString::fromUtf8("actionDelete_File"));
        actionSave = new QAction(IDEClass);
        actionSave->setObjectName(QString::fromUtf8("actionSave"));
        actionExit = new QAction(IDEClass);
        actionExit->setObjectName(QString::fromUtf8("actionExit"));
        actionSearch = new QAction(IDEClass);
        actionSearch->setObjectName(QString::fromUtf8("actionSearch"));
        actionSearch_and_Replace = new QAction(IDEClass);
        actionSearch_and_Replace->setObjectName(QString::fromUtf8("actionSearch_and_Replace"));
        actionPreferences = new QAction(IDEClass);
        actionPreferences->setObjectName(QString::fromUtf8("actionPreferences"));
        actionVersion = new QAction(IDEClass);
        actionVersion->setObjectName(QString::fromUtf8("actionVersion"));
        actionAbout = new QAction(IDEClass);
        actionAbout->setObjectName(QString::fromUtf8("actionAbout"));
        centralWidget = new QWidget(IDEClass);
        centralWidget->setObjectName(QString::fromUtf8("centralWidget"));
        gridLayout_2 = new QGridLayout(centralWidget);
        gridLayout_2->setSpacing(6);
        gridLayout_2->setContentsMargins(11, 11, 11, 11);
        gridLayout_2->setObjectName(QString::fromUtf8("gridLayout_2"));
        gridLayout = new QGridLayout();
        gridLayout->setSpacing(6);
        gridLayout->setObjectName(QString::fromUtf8("gridLayout"));
        treeView = new QTreeView(centralWidget);
        treeView->setObjectName(QString::fromUtf8("treeView"));

        gridLayout->addWidget(treeView, 0, 0, 1, 1);

        plainTextEdit = new QPlainTextEdit(centralWidget);
        plainTextEdit->setObjectName(QString::fromUtf8("plainTextEdit"));

        gridLayout->addWidget(plainTextEdit, 0, 1, 1, 1);

        gridLayout->setColumnStretch(0, 2);
        gridLayout->setColumnStretch(1, 5);

        gridLayout_2->addLayout(gridLayout, 0, 0, 1, 1);

        IDEClass->setCentralWidget(centralWidget);
        menuBar = new QMenuBar(IDEClass);
        menuBar->setObjectName(QString::fromUtf8("menuBar"));
        menuBar->setGeometry(QRect(0, 0, 600, 21));
        menuFile = new QMenu(menuBar);
        menuFile->setObjectName(QString::fromUtf8("menuFile"));
        menuEdit = new QMenu(menuBar);
        menuEdit->setObjectName(QString::fromUtf8("menuEdit"));
        menuTools = new QMenu(menuBar);
        menuTools->setObjectName(QString::fromUtf8("menuTools"));
        menuHelp = new QMenu(menuBar);
        menuHelp->setObjectName(QString::fromUtf8("menuHelp"));
        IDEClass->setMenuBar(menuBar);
        mainToolBar = new QToolBar(IDEClass);
        mainToolBar->setObjectName(QString::fromUtf8("mainToolBar"));
        IDEClass->addToolBar(Qt::TopToolBarArea, mainToolBar);
        statusBar = new QStatusBar(IDEClass);
        statusBar->setObjectName(QString::fromUtf8("statusBar"));
        IDEClass->setStatusBar(statusBar);

        menuBar->addAction(menuFile->menuAction());
        menuBar->addAction(menuEdit->menuAction());
        menuBar->addAction(menuTools->menuAction());
        menuBar->addAction(menuHelp->menuAction());
        menuFile->addAction(actionNew_Project);
        menuFile->addAction(actionLoad_Project);
        menuFile->addSeparator();
        menuFile->addAction(actionNew_File);
        menuFile->addAction(actionDelete_File);
        menuFile->addSeparator();
        menuFile->addAction(actionSave);
        menuFile->addAction(actionExit);
        menuEdit->addAction(actionSearch);
        menuEdit->addAction(actionSearch_and_Replace);
        menuTools->addAction(actionPreferences);
        menuHelp->addAction(actionVersion);
        menuHelp->addAction(actionAbout);

        retranslateUi(IDEClass);

        QMetaObject::connectSlotsByName(IDEClass);
    } // setupUi

    void retranslateUi(QMainWindow *IDEClass)
    {
        IDEClass->setWindowTitle(QApplication::translate("IDEClass", "IDE", 0, QApplication::UnicodeUTF8));
        actionNew_Project->setText(QApplication::translate("IDEClass", "New Project", 0, QApplication::UnicodeUTF8));
        actionLoad_Project->setText(QApplication::translate("IDEClass", "Load Project", 0, QApplication::UnicodeUTF8));
        actionNew_File->setText(QApplication::translate("IDEClass", "New File", 0, QApplication::UnicodeUTF8));
        actionDelete_File->setText(QApplication::translate("IDEClass", "Delete File", 0, QApplication::UnicodeUTF8));
        actionSave->setText(QApplication::translate("IDEClass", "Save", 0, QApplication::UnicodeUTF8));
        actionExit->setText(QApplication::translate("IDEClass", "Exit", 0, QApplication::UnicodeUTF8));
        actionSearch->setText(QApplication::translate("IDEClass", "Search", 0, QApplication::UnicodeUTF8));
        actionSearch_and_Replace->setText(QApplication::translate("IDEClass", "Search and Replace", 0, QApplication::UnicodeUTF8));
        actionPreferences->setText(QApplication::translate("IDEClass", "Preferences", 0, QApplication::UnicodeUTF8));
        actionVersion->setText(QApplication::translate("IDEClass", "Version", 0, QApplication::UnicodeUTF8));
        actionAbout->setText(QApplication::translate("IDEClass", "About", 0, QApplication::UnicodeUTF8));
        menuFile->setTitle(QApplication::translate("IDEClass", "File", 0, QApplication::UnicodeUTF8));
        menuEdit->setTitle(QApplication::translate("IDEClass", "Edit", 0, QApplication::UnicodeUTF8));
        menuTools->setTitle(QApplication::translate("IDEClass", "Tools", 0, QApplication::UnicodeUTF8));
        menuHelp->setTitle(QApplication::translate("IDEClass", "Help", 0, QApplication::UnicodeUTF8));
    } // retranslateUi

};

namespace Ui {
    class IDEClass: public Ui_IDEClass {};
} // namespace Ui

QT_END_NAMESPACE

#endif // UI_IDE_H
 
Zuletzt bearbeitet:
Du erbst von Ui::IDEClass, deine UI Klasse mit dem plainTextEdit Attribut heißt aber Ui_IDEClass?
Ja, runterscrollen.. :-(

Ach, jetzt seh ich erst das die setNewText Methode statisch ist. Das kann ja nicht funktionieren...
 
Zuletzt bearbeitet:

Neue Beiträge

Zurück