0

Can anyone tell me why I'm getting a segmentation fault in my printn function?

"grad.h"

namespace Ui {
class grad;
}

class grad : public QMainWindow
{
  Q_OBJECT

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

private:
Ui::grad *ui;
};

"course.cpp"

void course::printr(vector<course> c){
    QString string;
    for(int i = 0; i < (int)c.size();i++){
        string = QString::fromStdString(c[i].getTitle());
        Ui::grad->textEdit->append(string);
    }
}

The debugger shows the correct output up until the first iteration of the for loop when it reaches the Ui::grad part. then I get a segmentation fault. Let me know if I need to post more code thanks.

3 Answers3

1

I am not sure if textEdit is of Class QTextEdit. If so and you just want to append the text try

textEdit->setText(textEdit->plainText().append(string));
Sebastian Lange
  • 3,879
  • 1
  • 19
  • 38
  • I changed `Ui::grad->textEdit->append(string);` to `Ui::grad().textEdit->append("string1");` and now it will compile without errors but gives the segmentation fault when in run the program – Muhammad Abdusamad Aug 02 '13 at 15:19
1

Ui::grad->textEdit->append(string);
Error is here, but it shouldn't compile.
Ui::grad is the name of class, you cannot use operator -> to it. You definetly need some instance of grad class (not Ui::grad, just grad of your namespace) to do what you want.

Also it's generally not a good idea to name classes in the same manner as objects, I think you need to use some naming convention to make this kind of errors easier to find.

SpongeBobFan
  • 964
  • 5
  • 13
1

change line;

Ui::grad->textEdit->append(string);

to

ui->textEdit->append(string);

and let me know ifi it works or not.

Barış Akkurt
  • 2,255
  • 3
  • 22
  • 37
  • It won't work, `course` class have no member `ui` as I can predict – SpongeBobFan Aug 02 '13 at 09:17
  • yeah i didn't recognize this. he should use signals and slots to access main window members. – Barış Akkurt Aug 02 '13 at 09:18
  • SpongeBobFan is right. "course" has no member ui. The application has course.h, course.cpp, grad.h and grad.cpp. The grad class was generated by qt. I coded the course class by itself in c++ and now I'm trying to tie the two together. Instead of outputting text to the console, I want it to output to a textEdit. – Muhammad Abdusamad Aug 02 '13 at 15:14
  • This post [link](http://stackoverflow.com/questions/13313206/c-qt-uiwindow-and-inheritance-forward-declaration) seems to be dealing with the same problem. – Muhammad Abdusamad Aug 02 '13 at 15:31