将数据发送到另一个表单上的小部件

时间:2013-09-21 11:37:44

标签: c++ linux qt

本主题与此相关:

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

所以我创建了一些新对象:

class MainWindow : public QMainWindow
{
   Q_OBJECT

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

   DialogPreviewHeader PreviewHeader;
   Ui_DialogPreviewHeader UIDialog;

然后去使用它们:

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

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

}

以前,显示表单工作正常,现在似乎因使用UIDialog而崩溃。 textBrowserPreviewHeaderFile指针在Ui_DialogPreviewHeader类中设置,在调试时不为null。

另一个问题是学习如何连接mainWidget和amp;带有插槽的表格。

修改

当我这样做时:

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();

}

各种组合无法编译:

-> - PreviewHeader不是指针 .ui - 'Ui :: DialogPreviewHeader * DialogPreviewHeader :: ui'是私有的

使PreviewHeader成为指针并使用 - >表格也说DialogPreviewHeader :: ui'是私有的。

我也尝试过实现一个插槽,但另一种方式是将MainWindow改为DialogPreviewHeader:

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

但它不喜欢&MainWindow

任何方式 - 如果我发布所有代码会更好。我应该摆脱这个EDIT&把它贴在这里,还是做另一个问题?

好的 - 这是所有的代码 - 将来我会在开始时这样做!

#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()
   {

   }

您需要表格还是ui_标题?

编译器输出:

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.

1 个答案:

答案 0 :(得分:2)

以下行不正确:

 Ui_DialogPreviewHeader UIDialog;

你不被允许这样做。这条线就足够了:

DialogPreviewHeader PreviewHeader;

而不是:

UIDialog.textBrowserPreviewHeaderFile->setPlainText(SendTxt);

使用此:

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

就您的第二个问题而言,如果您想将小部件(mynewwidget)中的数据发送到主窗口,请尝试以下方法:

在窗口小部件(头文件)中创建一个信号,例如:

信号:         void myData(QString);

现在假设您要将小部件(mynewwidget)中的字符串数据发送到主窗口,现在只需调用myData就像一个简单的函数,您可以从mynewwidget类中发送数据,例如你想发送“你好”:

myData("Hello");

现在,在MainWindow类的构造函数中,您需要将信号myData(QString)与MainWindow的任何插槽连接,并使用与widget类信号相同的签名。在mainwindow的构造函数中,您需要为连接添加此行:

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

其中myRData是MainWindow中的一个插槽。插槽只是可以直接调用以及使用信号调用的函数。在主窗口类中,一个插槽将是这样的:

Slots:
 myRData(QString);

现在每当调用myData时,MyRData也会被调用。请注意,信号没有函数体,只能通过它们传递数据。

编辑:示例

#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”//再也不这样做了    // #include“ui_MainWindow.h”//这也是

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-&GT; textBrowserPreviewHeaderFile-&GT; 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()
   {

   }