TheIdeasMan
TheIdeasMan

Reputation: 77

Send data to widget on another form

This topic is related to this one:

Qt Creator 2.8.1 Qt 5.1.1 Qt Designer Linux Show a new Form

So I created some new objects:

class MainWindow : public QMainWindow
{
   Q_OBJECT

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

   DialogPreviewHeader PreviewHeader;
   Ui_DialogPreviewHeader UIDialog;

Then went to use them like this:

void MainWindow::on_pushButtonPreviewHeader_clicked()
{
   QString SendTxt = ui->lineEditClassName->text();

   UIDialog.textBrowserPreviewHeaderFile->setPlainText(SendTxt);
   PreviewHeader.show();

}

Previously, showing the Form worked, now it seems to crash with the use of UIDialog. The textBrowserPreviewHeaderFile pointer was set in the Ui_DialogPreviewHeader class and is not null when debugging.

The other question was to learn how to connect a mainWidget & a Form with Slots.

EDIT:

When I do this:

void MainWindow::on_pushButtonPreviewHeader_clicked()
{
   QString SendTxt = ui->lineEditClassName->text();



   //UIDialog.textBrowserPreviewHeaderFile->setPlainText(SendTxt);
   PreviewHeader.ui->textBrowserPreviewHeaderFile->setPlainText(SendTxt);
   //PreviewHeader->ui->textBrowserPreviewHeaderFile->setPlainText(SendTxt);

   //pushButtonPreviewHeader(SendTxt);
   PreviewHeader->show();

}

Various combinations don't compile:

The -> -PreviewHeader is not a pointer The .ui - 'Ui::DialogPreviewHeader* DialogPreviewHeader::ui' is private

Making PreviewHeader a pointer and using the -> form also says that DialogPreviewHeader::ui' is private.

I also tried implementing a slot, but the other way around MainWindow to DialogPreviewHeader:

DialogPreviewHeader::DialogPreviewHeader(QWidget *parent) :
   QDialog(parent),
   ui(new Ui::DialogPreviewHeader)
{
   ui->setupUi(this);
   connect(
       &MainWindow,
       SIGNAL(pushButtonPreviewHeader(QString)),
       this,
       SLOT(ReceiveDataFromMainWidget(QString)));
}

But it didn't like the &MainWindow.

Any way - would it be much better if I posted all the code. Should I get rid of this EDIT & post it here, or do yet another Question?

OK - here is all the code - in future I will do this at the start !!

#ifndef DIALOGPREVIEWHEADER_H
#define DIALOGPREVIEWHEADER_H

#include <QDialog>

namespace Ui {
class DialogPreviewHeader;
}

class DialogPreviewHeader : public QDialog
{
   Q_OBJECT

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



private slots:
   void on_buttonBox_accepted();

   void ReceiveDataFromMainWidget(QString);

private:
   Ui::DialogPreviewHeader *ui;
};

#endif // DIALOGPREVIEWHEADER_H

MainWindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

#include "DialogPreviewHeader.h"
#include "ui_DialogPreviewHeader.h"

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
   Q_OBJECT

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

  //DialogPreviewHeader PreviewHeader;
  //Ui_DialogPreviewHeader UIDialog;

private slots:
   void on_pushButtonPreviewHeader_clicked();

   void pushButtonPreviewHeader(QString);

   void on_pushButtonPreviewImplFile_clicked();

   void on_pushButtonWriteFiles_clicked();

   void on_lineEditNSNames_textChanged(const QString &arg1);

   void on_radioButtonNSIndividual_clicked();

   void on_radioButtonOneNSAlias_clicked();

   void on_lineEditClassName_textChanged(const QString &Text);


   void on_lineEditClassNamePrepend_textEdited(const QString &arg1);

   void on_pushButtonCancel_clicked();

   void on_checkBoxPrependCharClassName_clicked();

   private:
       Ui::MainWindow *ui;
       DialogPreviewHeader *PreviewHeader;
       //Ui_DialogPreviewHeader.textBrowserPreviewHeaderFile *pHeader;
       //textBrowserPreviewHeaderFile *pHeader;
   };

   #endif // MAINWINDOW_H

DialogPreviewHeader.h

#include "DialogPreviewHeader.h"
#include "ui_DialogPreviewHeader.h"

#include "MainWindow.h"
#include "ui_MainWindow.h"

DialogPreviewHeader::DialogPreviewHeader(QWidget *parent) :
   QDialog(parent),
   ui(new Ui::DialogPreviewHeader)
{
   ui->setupUi(this);
   connect(
       Ui::MainWindow,
       SIGNAL(pushButtonPreviewHeader(QString)),
       this,
       SLOT(ReceiveDataFromMainWidget(QString)));
}

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



void DialogPreviewHeader::on_buttonBox_accepted()
{
    this->hide();
}

main.cpp

#include "MainWindow.h"
#include <QApplication>

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

   return a.exec();
}

MainWindow.cpp

#include "MainWindow.h"
#include "ui_MainWindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent), ui(new Ui::MainWindow)
{
   ui->setupUi(this);

}

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

void MainWindow::on_pushButtonPreviewHeader_clicked()
{
   QString SendTxt = ui->lineEditClassName->text();



   //UIDialog.textBrowserPreviewHeaderFile->setPlainText(SendTxt);
   //PreviewHeader.ui->textBrowserPreviewHeaderFile->setPlainText(SendTxt);
   //PreviewHeader->ui->textBrowserPreviewHeaderFile->setPlainText(SendTxt);

   pushButtonPreviewHeader(SendTxt);
   PreviewHeader->show();

}

void MainWindow::pushButtonPreviewHeader(QString) {

}

void MainWindow::on_pushButtonPreviewImplFile_clicked()
{

}

void MainWindow::on_pushButtonWriteFiles_clicked()
{

}

void MainWindow::on_lineEditNSNames_textChanged(const QString &arg1)
{

}

void MainWindow::on_radioButtonNSIndividual_clicked()
{

}

void MainWindow::on_radioButtonOneNSAlias_clicked()
{

}

void MainWindow::on_lineEditClassName_textChanged(const QString &Text)
{

}

void MainWindow::on_lineEditClassNamePrepend_textEdited(const QString &arg1)
{

}

void MainWindow::on_pushButtonCancel_clicked()
   {
  this->close();
   }

void MainWindow::on_checkBoxPrependCharClassName_clicked()
   {

   }

Do you need the forms or the ui_ headers?

The compiler output:

04:48:04: Running steps for project ClassWiz...
04:48:04: Configuration unchanged, skipping qmake step.
04:48:04: Starting: "/usr/bin/make" 
g++ -c -pipe -std=c++11 -Wall -Wextra -pedantic -g -Wall -W -D_REENTRANT -fPIE -DQT_QML_DEBUG -DQT_DECLARATIVE_DEBUG -DQT_WIDGETS_LIB -DQT_GUI_LIB -DQT_CORE_LIB -I/opt/Qt5.1.1/5.1.1/gcc_64/mkspecs/linux-g++ -I../ClassWiz -I/opt/Qt5.1.1/5.1.1/gcc_64/include -I/opt/Qt5.1.1/5.1.1/gcc_64/include/QtWidgets -I/opt/Qt5.1.1/5.1.1/gcc_64/include/QtGui -I/opt/Qt5.1.1/5.1.1/gcc_64/include/QtCore -I. -I. -I. -o DialogPreviewHeader.o ../ClassWiz/DialogPreviewHeader.cpp
../ClassWiz/DialogPreviewHeader.cpp: In constructor 'DialogPreviewHeader::DialogPreviewHeader(QWidget*)':
../ClassWiz/DialogPreviewHeader.cpp:12:26: error: expected primary-expression before ',' token
make: *** [DialogPreviewHeader.o] Error 1
04:48:05: The process "/usr/bin/make" exited with code 2.
Error while building/deploying project ClassWiz (kit: Desktop Qt 5.1.1 GCC 64bit)
When executing step 'Make'
04:48:05: Elapsed time: 00:01.

Upvotes: 0

Views: 2082

Answers (1)

adnan kamili
adnan kamili

Reputation: 9465

Following line is incorrect:

 Ui_DialogPreviewHeader UIDialog;

You are not allowed to do that. This line is enough:

DialogPreviewHeader PreviewHeader;

And instead of:

UIDialog.textBrowserPreviewHeaderFile->setPlainText(SendTxt);

Use this:

 PreviewHeader->ui->textBrowserPreviewHeaderFile->setPlainText(SendTxt);

As far as your second question is concerned, if you want to send data from a widget (mynewwidget) to the main window try this:

Create a signal in your widget (header file), e.g:

signals: void myData(QString);

Now suppose you want to send a string data from widget (mynewwidget) to main window, now just call the myData like a simple function, from where you want to send the data in your mynewwidget class, e.g. you want to send "hello":

myData("Hello");

Now, in the constructor of MainWindow class you need to connect the signal myData(QString) with any of the slots of the MainWindow with the same signature as that of signal of widget class. In constructor of mainwindow, you will need to add this line for connection:

connect(&mynewwidget,SIGNAL(myData(QString)),this,SLOT(myRData(QString)));

Where myRData is a slot in MainWindow. Slots are simply functions which can be invoked directly as well as using signals. In mainwindow class a slot will be like this:

Slots:
 myRData(QString);

Now whenever myData is invoked, MyRData will also get invoked. Please note signals don't have a function body, you only pass on the data through them.

Edit: Example

#ifndef DIALOGPREVIEWHEADER_H
#define DIALOGPREVIEWHEADER_H

#include <QDialog>

namespace Ui {
class DialogPreviewHeader;
}

class DialogPreviewHeader : public QDialog
{
   Q_OBJECT

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



private slots:
   void on_buttonBox_accepted();

   //void ReceiveDataFromMainWidget(QString);                // not needed

public:                              // made public
   Ui::DialogPreviewHeader *ui;
};

#endif // DIALOGPREVIEWHEADER_H

MainWindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

#include "DialogPreviewHeader.h"
#include "ui_DialogPreviewHeader.h"

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
   Q_OBJECT

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

  DialogPreviewHeader PreviewHeader;

private slots:
   void on_pushButtonPreviewHeader_clicked();

   void pushButtonPreviewHeader(QString);

   void on_pushButtonPreviewImplFile_clicked();

   void on_pushButtonWriteFiles_clicked();

   void on_lineEditNSNames_textChanged(const QString &arg1);

   void on_radioButtonNSIndividual_clicked();

   void on_radioButtonOneNSAlias_clicked();

   void on_lineEditClassName_textChanged(const QString &Text);


   void on_lineEditClassNamePrepend_textEdited(const QString &arg1);

   void on_pushButtonCancel_clicked();

   void on_checkBoxPrependCharClassName_clicked();

   private:
       Ui::MainWindow *ui;
       //DialogPreviewHeader *PreviewHeader;                         not needed
       //Ui_DialogPreviewHeader.textBrowserPreviewHeaderFile *pHeader;
       //textBrowserPreviewHeaderFile *pHeader;
   };

   #endif // MAINWINDOW_H

DialogPreviewHeader.h

#include "DialogPreviewHeader.h"
#include "ui_DialogPreviewHeader.h"

// #include "MainWindow.h" // never do this again // #include "ui_MainWindow.h" // this too

DialogPreviewHeader::DialogPreviewHeader(QWidget *parent) :
   QDialog(parent),
   ui(new Ui::DialogPreviewHeader)
{
   ui->setupUi(this);
   /*connect(                                     // never do this again
       Ui::MainWindow,
       SIGNAL(pushButtonPreviewHeader(QString)),
       this,
       SLOT(ReceiveDataFromMainWidget(QString)));*/
}

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



void DialogPreviewHeader::on_buttonBox_accepted()
{
    this->hide();
}

main.cpp

#include "MainWindow.h"
#include <QApplication>

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

   return a.exec();
}

MainWindow.cpp

#include "MainWindow.h"
#include "ui_MainWindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent), ui(new Ui::MainWindow)
{
   ui->setupUi(this);

}

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

void MainWindow::on_pushButtonPreviewHeader_clicked()
{
   QString SendTxt = ui->lineEditClassName->text();


//Make sure  Ui::DialogPreviewHeader *ui; is public in dialogpreviewheader.h

PreviewHeader.ui->textBrowserPreviewHeaderFile->setPlainText(SendTxt);

   pushButtonPreviewHeader(SendTxt);
   PreviewHeader->show();

}

void MainWindow::pushButtonPreviewHeader(QString) {

}

void MainWindow::on_pushButtonPreviewImplFile_clicked()
{

}

void MainWindow::on_pushButtonWriteFiles_clicked()
{

}

void MainWindow::on_lineEditNSNames_textChanged(const QString &arg1)
{

}

void MainWindow::on_radioButtonNSIndividual_clicked()
{

}

void MainWindow::on_radioButtonOneNSAlias_clicked()
{

}

void MainWindow::on_lineEditClassName_textChanged(const QString &Text)
{

}

void MainWindow::on_lineEditClassNamePrepend_textEdited(const QString &arg1)
{

}

void MainWindow::on_pushButtonCancel_clicked()
   {
  this->close();
   }

void MainWindow::on_checkBoxPrependCharClassName_clicked()
   {

   }

Upvotes: 2

Related Questions