1
votes

I'd like to access some functions of a dialog-type widget from my mainwindow.cpp.

I've created a simple function in the widget class that returns an int, which looks like this:

dialog.h:

 public:  
      int getRowCountData();

dialog.cpp:

 int Dialog::getRowCountData()
 {
      return ui->tableWidget->rowCount();
 }

Usage:

my mainWindow.h:

private:
    Dialog *dialog;

my mainwindow.cpp:

dialog = new Dialog;


int count = dialog->getRowCountData();   <<<<<--------------this throws a read access error!

code: 0xc0000005 read access violation at: 0x0 flas=0x0

how am I to use another widget's public functions such as this simple one?

I must not be referencing the integer I'd like to set using the rowcount function. Signals and slots have worked great for me in the past when using them to transfer data between widgets, but I'd like to stick to using my dialog widget's function like this if I can.

I came about this method for retrieving data on my other widget by posting this question: AccessingQTableWidget's data from another class. @Chernobyl maybe you have some further insight to provide?

Thanks in advance!

1
Question needs more code, problem is not visible now. - hyde
Also, bcaktrace would be good. If it is the line you now indicate in the question, dialog pointer there is 0. Why, is not apparent from the question. - hyde
In Qt Creator, go to the lines with new Dialog and dialog->, and hit F2 on both dialog names. Do they take you to the same place (probably the declaration in .h file of the class)? Set breakpoints or add debug prints for both to see if dialog really is created before you try to use it. - hyde

1 Answers

2
votes

No. Problem in usingwrong pointer. This execption means exactly this.

Check is dialog pointer isn't null and maybe ui->tableWidget doesn't exist. You should call setupUi if you want use widgets which created in Qt Designer

Write this:

if(!dialog)
qDebug() << "fail";
else
{qDebug() << "good";//your call}

Most times such a crash happens is caused by

  • deleting an object more than once
  • accessing a dangling pointer (i.e. a pointer to an already deleted object)
  • accessing a null pointer