This forum has been archived. All content is frozen. Please use KDE Discuss instead.

[SOLVED] Qt: Access GUI from another class

Tags: None
(comma "," separated)
User avatar
mensch
Registered Member
Posts
178
Karma
0
OS
I think I've tried a similar approach earlier, because I'm getting the same errors now as I got then. I didn't post my main.cpp code earlier, but this is where the errors start now. Understandably, because MainWindow should be called with two arguments this way, only I'm not sure what to pass in main.cpp:

main.cpp:
Code: Select all
#include
#include "mainwindow.h"
#include "datamodel.h"

int main(int argc, char *argv[])
{
  QApplication myApp(argc, argv);

  MainWindow mainWindow;
  mainWindow.show();

  return myApp.exec();
}

This is the error:
Code: Select all
error: no matching function for call to 'MainWindow::MainWindow()


Other errors crop up in mainwindow.cpp:
Code: Select all
error: default argument missing for parameter 2 of 'MainWindow::MainWindow(QWidget*, dataModel*)'


And datamodel.cpp:
Code: Select all
error: no matching function for call to 'MainWindow::MainWindow(dataModel* const, dataModel* const)'

Last edited by mensch on Thu Apr 09, 2009 11:35 pm, edited 1 time in total.


I have forced myself to contradict myself in order to avoid conforming to my own taste. Marcel Duchamp
User avatar
bcooksley
Administrator
Posts
19765
Karma
87
OS
You have two choices really:
1: Make main.cpp create DataModel, which will then create the MainWindow.
2: Make main.cpp create MainWindow, which will create the DataModel.

Regarding error in datamodel.cpp, oops my mistake. It should have been this:
Code: Select all
mainWindow = new MainWindow(0, this);


Also, try this for a constructor:
Code: Select all
MainWindow(QWidget* parent = 0, DataModel* parentModel = DataModel(this));


KDE Sysadmin
[img]content/bcooksley_sig.png[/img]
User avatar
mensch
Registered Member
Posts
178
Karma
0
OS
I think option two is the best one. Create the mainwindow first and then populate it seems more logical.

When I put dataModel* parentModel = dataModel(this) in the constructor I get the error that you can't use "this" in this context.

There are other errors, but we're down to 9 instead of 21. ;) One is the "error: default argument missing for parameter 2 of 'MainWindow::MainWindow(QWidget*, dataModel*)'" and the others have to do with a failure to make MainWindow privately or publicly available in datamodel.h for other functions (error: ISO C++ forbids declaration of 'MainWindow' with no type).

Last edited by mensch on Fri Apr 10, 2009 9:17 am, edited 1 time in total.


I have forced myself to contradict myself in order to avoid conforming to my own taste. Marcel Duchamp
User avatar
bcooksley
Administrator
Posts
19765
Karma
87
OS
Given your choice.....
The MainWindow constructor no longer requires DataModel in it:
Code: Select all
MainWindow(QWidget* parent = 0)


The DataModel constructor needs to mention MainWindow now:
Code: Select all
dataModel::dataModel(MainWindow* parent)


You can fix the forbids declaration.... error by adding the following line around the top of datamodel.h:
Code: Select all
#include "mainwindow.h"


KDE Sysadmin
[img]content/bcooksley_sig.png[/img]
User avatar
mensch
Registered Member
Posts
178
Karma
0
OS
I think I'm still doing something wrong. Sorry. :-( Hope I'm not testing your patience too much. :-$

Have I missed something in the declarations below?

I get the following errors:

datamodel.cpp:31: error: prototype for 'dataModel::dataModel(MainWindow*)' does not match any in class 'dataModel'
error: dataModel::dataModel()
error: candidates are: dataModel::dataModel(const dataModel& )
datamodel.h:37: error: expected `)' before '*' token


mainwindow.cpp:
Code: Select all
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent)
{
  setupUi(this);
}


Code: Select all
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include
#include "ui_mainwindow.h"

class dataModel;

class MainWindow : public QMainWindow, public Ui::MainWindow
{
  Q_OBJECT

  public:
    MainWindow(QWidget* parent = 0);
};

#endif


datamodel.cpp:
Code: Select all
#include "datamodel.h"
#include "mainwindow.h"
#include
#include
#include
#include

dataModel::dataModel(MainWindow* parent)
{
  qDebug() clientList->objectName();
}

datamodel.h:

Code: Select all
#ifndef DATAMODEL_H
#define DATAMODEL_H

#include
#include
#include
#include

class dataModel
{
  public:
    dataModel(MainWindow* parent);
};
#endif


I have forced myself to contradict myself in order to avoid conforming to my own taste. Marcel Duchamp
User avatar
bcooksley
Administrator
Posts
19765
Karma
87
OS
You need to add this to datamodel.h:
Code: Select all
class MainWindow;


KDE Sysadmin
[img]content/bcooksley_sig.png[/img]
User avatar
mensch
Registered Member
Posts
178
Karma
0
OS
Thanks again! Everything seems to work perfectly now. No errors and I'm able to access the things I want.

Thanks bcooksley and anda_skoa for all the help.


I have forced myself to contradict myself in order to avoid conforming to my own taste. Marcel Duchamp
User avatar
mensch
Registered Member
Posts
178
Karma
0
OS
Back again with a related question. Is it possible to use slots defined in the datamodel class in the mainwindow class or is it considered bad practice?


I have forced myself to contradict myself in order to avoid conforming to my own taste. Marcel Duchamp
User avatar
bcooksley
Administrator
Posts
19765
Karma
87
OS
It is completely acceptable to use a childs signals / slots to do something, I tend to avoid using a parents signals / slots however.


KDE Sysadmin
[img]content/bcooksley_sig.png[/img]
User avatar
mensch
Registered Member
Posts
178
Karma
0
OS
I'm sure I'm pointing to the wrong object again, because I get these warnings when declaring the slot like this. "model" is the publicly available dataModel class declared in mainwindow.h:
Code: Select all
connect(clientList, SIGNAL(itemClicked(QTreeWidgetItem*, int)), model, SLOT(updateProjectData(QTreeWidgetItem*)));

Warning:
Code: Select all
Object::connect: No such slot QObject::updateProjectData(QTreeWidgetItem*)


Data model is declared like this:
Code: Select all
dataModel::dataModel(QObject* parent, MainWindow* mainWindow) : QObject(parent)


I have forced myself to contradict myself in order to avoid conforming to my own taste. Marcel Duchamp
User avatar
bcooksley
Administrator
Posts
19765
Karma
87
OS
You need to connect to it like so, since the mainWindow owns the object you are connecting to.

connect(clientList, SIGNAL(itemClicked(QTreeWidgetItem*, int)), mainWindow->model, SLOT(updateProjectData(QTreeWidgetItem*)));


KDE Sysadmin
[img]content/bcooksley_sig.png[/img]
User avatar
mensch
Registered Member
Posts
178
Karma
0
OS
mainWindow is only declared in datamodel.h, so I get an error on scope declaration. I'm calling the connect in mainwindow.cpp, while the slot function is in datamodel.cpp.


I have forced myself to contradict myself in order to avoid conforming to my own taste. Marcel Duchamp
User avatar
bcooksley
Administrator
Posts
19765
Karma
87
OS
Oops. I misread the post. I should have put:
connect(clientList, SIGNAL(itemClicked(QTreeWidgetItem*, int)), dataModel->model, SLOT(updateProjectData(QTreeWidgetItem*)));


KDE Sysadmin
[img]content/bcooksley_sig.png[/img]
User avatar
mensch
Registered Member
Posts
178
Karma
0
OS
Tried that one as well. ;) But I get the error:
Code: Select all
error: expected primary-expression before '->' token

It's being declared like this in mainwindow.h:
Code: Select all
class MainWindow : public QMainWindow, public Ui::MainWindow
{
  Q_OBJECT

  public:
    MainWindow(QWidget* parent = 0);

  protected:
    dataModel* model;
};

And called in mainwindow.cpp:
Code: Select all
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent)
{
  setupUi(this);
  model = new dataModel(0, this);
}


I have forced myself to contradict myself in order to avoid conforming to my own taste. Marcel Duchamp
User avatar
bcooksley
Administrator
Posts
19765
Karma
87
OS
Maybe try this instead:
connect(clientList, SIGNAL(itemClicked(QTreeWidgetItem*, int)), model->(**name of item as specified in ui file**), SLOT(updateProjectData(QTreeWidgetItem*)));

Make sure to replace the entire (**name of item as specified in ui file**)
with what is supposed to be there

Last edited by bcooksley on Wed Apr 15, 2009 8:43 pm, edited 1 time in total.


KDE Sysadmin
[img]content/bcooksley_sig.png[/img]


Bookmarks



Who is online

Registered users: Bing [Bot], Google [Bot], Sogou [Bot]