Muhammad Abdusamad
Muhammad Abdusamad

Reputation: 39

Segmentation fault in qt

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.

Upvotes: 0

Views: 947

Answers (3)

Barış Akkurt
Barış Akkurt

Reputation: 2256

change line;

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

to

ui->textEdit->append(string);

and let me know ifi it works or not.

Upvotes: 1

SpongeBobFan
SpongeBobFan

Reputation: 964

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.

Upvotes: 1

Sebastian Lange
Sebastian Lange

Reputation: 4029

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));

Upvotes: 1

Related Questions