【发布时间】:2013-07-02 21:34:01
【问题描述】:
我正在尝试访问 MainWindow 类中私有的 ui 成员。
当我从 gl 小部件释放鼠标按钮(鼠标移动的量)时,我想更新 lineEdit (Xvaldisp)。
经过一番搜索,我发现我需要在主窗口中创建一个函数/方法 然后通过我的 GLWidget 中指向 Mainwindow 的指针访问它
问题:
lineEdit 保持空白,应该更新它的方法( displaymessage() )似乎被调用了。
为了检查我是否创建了一个字符串 (str) 以查看是否调用了 displaymessage,当调用 displaymessage() 时,此字符串将更新为一个新值。
displaymessage() 下面的 on_Button_clicked() 方法也会在单击按钮时更新相同的 lineEdit 并且工作正常 它显示 str 的内容
这是我的代码:
glwidget.h
#ifndef GLWIDGET_H
#define GLWIDGET_H
#include <QGLWidget>
#include <QTimer>
#include <QMouseEvent>
#include "mainwindow.h"
#include <QObject>
#include <QLineEdit>
class GLWidget : public QGLWidget
{
Q_OBJECT
public:
explicit GLWidget(QWidget *parent = 0);
void mousePressEvent(QMouseEvent *e);
void mouseReleaseEvent(QMouseEvent *e);
void initializeGL();
void paintGL();
void resizeGL(int w, int h);
private:
QTimer timer;
QPoint pointMpressed;
QPoint diff;
protected:
signals:
void valueCh();
};
#endif // GLWIDGET_H
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QLineEdit>
#include <QObject>
#include "glwidget.h"
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow {
Q_OBJECT
public:
MainWindow(QWidget *parent = 0);
~MainWindow();
public slots:
void on_Button_clicked();
void displayMessage();
protected:
void changeEvent(QEvent *e);
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
glwidget.cpp.
#include "glwidget.h"
#include <GL/glut.h>
GLWidget::GLWidget(QWidget *parent) :
QGLWidget(parent)
{
connect (&timer,SIGNAL(timeout()),this,SLOT(updateGL()));
timer.start(16);
}
void GLWidget::mousePressEvent(QMouseEvent *e){
pointMpressed=e->pos();
}
void GLWidget::mouseReleaseEvent(QMouseEvent *e){
diff=(e->pos())- pointMpressed ; //calculate position difference between click and release
MainWindow *mwd;
mwd= new MainWindow;
// mwd->displayMessage(); //tried this first then with signals and slots below same result
QObject::connect(this, SIGNAL(valueCh()), mwd ,SLOT(displayMessage()) );
emit valueCh();
delete mwd;
}
void GLWidget::initializeGL(){
}
void GLWidget::resizeGL(int w, int h){
}
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "glwidget.h"
QString str="none activated"; //I used this string to check if the methods were getting called
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::changeEvent(QEvent *e)
{
QMainWindow::changeEvent(e);
switch (e->type()) {
case QEvent::LanguageChange:
ui->retranslateUi(this);
break;
default:
break;
}
}
void MainWindow::displayMessage(){ //this method should update the lineEdit (Xvaldisp) the lineEdit however remains blank but str gets updated
ui->Xvaldisp->setText(str);
str ="displayMessage hs rn"; //displaymessage has run
}
void MainWindow::on_Button_clicked(){ // this is a pushbutton(Button) that once pushed also updates the same lineEdit(Xvaldisp) this works just fine If I clicked and released the mouse before on the GLWidget it would show the by displaymessage() updated string else it would be the orginal value
ui->Xvaldisp->setText(str);
}
【问题讨论】:
-
已初始化 mwd。从你的代码看来不是。
-
您需要将指向现有 MainWindow 的指针传递给 GLWidget
-
感谢 cmets 我该怎么做?真的不熟悉cpp类需要尽快阅读
-
您可能会遇到上述初始化问题...但是您还应该考虑将
signal从GLWidget连接到MainWindow中的slot,而不是传递指针.