Hi all,
I just started learning Qt and was working myself through a few tutorials. I have a basic knowledge of c++, but not too much either.
I've written a small programm. In this program I have a class with a QTextEdit object. This class has a member function which I want to add some text to the QTextEdit object. But I seem to be unable to get it working. I guess the problem lies where I call the particular function, but to me this looks correct.
Here is the complete code, the problematic line (line 15) is put as comment ("There is a problem"):
main.cpp:
#include "MyWidget.h"
#include <QApplication>
#include <QVBoxLayout>
#include <QWidget>
#include <QTextEdit>
// Main function
int main(int argc, char *argv[])
{
MyWidget widget;
widget.show();
return app.exec();
}
#include "MyWidget.h"
#include <QApplication>
#include <QVBoxLayout>
#include <QWidget>
#include <QTextEdit>
// Main function
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
MyWidget widget;
widget.show();
return app.exec();
}
To copy to clipboard, switch view to plain text mode
MyWidget.h
#ifndef MYWIDGET_H
#define MYWIDGET_H
#include <QApplication>
#include <QVBoxLayout>
#include <QWidget>
#include <QTextEdit>
{
Q_OBJECT
public:
~MyWidget();
};
#endif
#ifndef MYWIDGET_H
#define MYWIDGET_H
#include <QApplication>
#include <QVBoxLayout>
#include <QWidget>
#include <QTextEdit>
class MyWidget : public QWidget
{
Q_OBJECT
public:
MyWidget(QWidget *parent = 0);
~MyWidget();
void addtexttofield(QTextEdit*);
};
#endif
To copy to clipboard, switch view to plain text mode
MyWidget.cpp
#include "MyWidget.h"
// MyWidget class destructor
MyWidget::~MyWidget()
{}
// MyWidget class constructor
MyWidget
::MyWidget(QWidget *parent
) {
// There is a problem
// addtexttofield(textfield);
// Layout
layout->addWidget(textfield);
setLayout(layout);
}
// A function to modify QTextEdit content
{
zieltextfeld -> setText("This is something new");
}
#include "MyWidget.h"
// MyWidget class destructor
MyWidget::~MyWidget()
{}
// MyWidget class constructor
MyWidget::MyWidget(QWidget *parent)
: QWidget(parent)
{
QTextEdit *textfield = new QTextEdit();
// There is a problem
// addtexttofield(textfield);
// Layout
QVBoxLayout *layout = new QVBoxLayout;
layout->addWidget(textfield);
setLayout(layout);
}
// A function to modify QTextEdit content
void addtexttofield(QTextEdit *zieltextfeld)
{
zieltextfeld -> setText("This is something new");
}
To copy to clipboard, switch view to plain text mode
Thanx in advance.
Tl;dr: How do I call function addtexttofield(QTextEdit*)?
Bookmarks