Accessing widgets from another form
-
I'm pretty new to c++ and qt. I want to make a debug console which is accessible from my main window and all other dialogs in my application
My debug console has an QPlainTextEdit widget on it here's the code for debugConsole.h:
@#ifndef DEBUGCONSOLE_H
#define DEBUGCONSOLE_H#include <QString>
#include <QDialog>namespace Ui {
class debugConsole;
}class debugConsole : public QDialog
{
Q_OBJECTpublic:
explicit debugConsole(QWidget *parent = 0);
~debugConsole();void sendDbgOutput(QString output);
private:
Ui::debugConsole *ui;
};#endif // DEBUGCONSOLE_H@
and here's the code for debugConsole.cpp:
@#include "debugconsole.h"
#include "ui_debugconsole.h"debugConsole::debugConsole(QWidget *parent) :
QDialog(parent),
ui(new Ui::debugConsole)
{
Qt::WindowFlags flags = windowFlags() ;
flags |= Qt::CustomizeWindowHint ;
flags &= ~Qt::WindowCloseButtonHint ;
setWindowFlags(flags);
ui->setupUi(this);
}debugConsole::~debugConsole()
{
delete ui;
}void debugConsole::sendDbgOutput(QString output)
{
ui->debugOutput->appendPlainText(output);
}
@from my mainwindow.cpp I include debugConsole.h like such:
@#include "debugconsole.h"@then, I do this here:
@debugConsole *dbgConsole;@now when I try to call it like this, my program crashes:
@void MainWindow::captureEnabled()
{
if(ui->captureSlider->value() == 1)
{
capturing = true;
ui->captureLabel->setText("YES");
ui->captureLabel->repaint();
dbgConsole->sendDbgOutput("Capture Enabled");
}
else
{
capturing = false;
ui->captureLabel->setText("NO");
ui->captureLabel->repaint();
}
}@what am i doing wrong/not doing at all?
-
@debugConsole *dbgConsole = new debugConsole(this);@
instead of:
@debugConsole *dbgConsole;@ -
ok, I know i'm going to look pretty stupid for this.
but when I put that at the top of mainwindow.cpp
I get the error, "invalid use of 'this' at top level"
but, if i put it inside of any other function it won't let me use it?
-
in your debugConsole constructor, QWidget pointer is needed as parent widget. I have assumed what you are instantiating debugConsole instance inside some kind of main widget class.
@debugConsole::debugConsole(QWidget *parent)@You can create instance of debugConsole without setting parent QWidget pointer.
@debugConsole *dbgConsole = new debugConsole(NULL);@ -
i tried that but, I get a microsoft visual c++ runtime error
-
bq. i tried that but, I get a microsoft visual c++ runtime error
It has nothing todo with debugConsole constructor... Code which causes program to fail is somewhere else...
@debugConsole::debugConsole(QWidget *parent) :
QDialog(parent),
ui(new Ui::debugConsole)
{
Qt::WindowFlags flags = windowFlags() ;
flags |= Qt::CustomizeWindowHint ;
flags &= ~Qt::WindowCloseButtonHint ;
setWindowFlags(flags);
ui->setupUi(this);
}@ -
ahhh, well thanks..
Instead of reading a book I decided to jump right into programming and ask for help when I get stuck, so I'm sorry for being kind of an idiot.
-
No problem... There's nothing shameful about being a beginner....
-
My solution is to make "ui" public:
//sub window: Form3PType1 public: Ui::Form3PType1 *ui; //main window: FormCL3P qDebug() << "ANo: " << G::form3PType1->ui->tbxANo->toPlainText();
output: ANo: "A5"
When the "ui" of a window is public, it can be accessed from other cpp.
Works for me.