2

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!

3
  • Question needs more code, problem is not visible now. Commented Sep 18, 2014 at 4:36
  • 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. Commented Sep 18, 2014 at 6:38
  • 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. Commented Sep 18, 2014 at 6:40

1 Answer 1

2

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
Sign up to request clarification or add additional context in comments.

5 Comments

Setupui is called, and I was easily able to see that getRowCountData() was a member of the dialog instance on main window. No problems there.
@Rachael see my edit. I told you about dialog too. Rum with this code. Did you get fail or good?
I see now what you guys are saying, and I do feel like a butthead. Will add the null checks for dialog ASAP (first thing in the morning.) Thank you for sticking with me here.
the issue was that the function running this code was called before the dialog widget had been instantiated. The dialog pointer wasn't null--it was filled with garbage (I had already initialized in in my mainwindow.h class, so there was no way it could be null). Sorry for the dumb mistake, but thanks for your help. I won't be making this error again (I hope). Next time I will sleep before debugging and posting my issues on the site. Thanks again.
@Rachael You are welcome. But I have one more suggestion. In future create your questions with c++ tag please (of course if you use c++ in your question). In addition, edit your questions(this and your last) and add c++ tag to them (if you can do it), because your questions are more c++,then Qt, so c++ tag should be.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.