Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 404
  • Last Modified:

QT app crashes.

Hello:

I intend to have the following use case scenario using QT:

A Qialog is to be composed of  a QPushButton and a widget derived from QTAbleWIdget. Each time  the button is pushed a new row gets added to the table. Each row is comprised of 4 columns. The first column contains a QCheckBox, the second a QComboBox and text for the 3rd and 4th column, I am attaching the code below.
/* myTable.h

# ifndef __MTBL__
# define __MTBL__

# include <QTableWidget>
# include <QPushButton>
# include <QComboBox>
# include <QDialog>
# include <QCheckBox>

class myTable : public QTableWidget
{
	Q_OBJECT
public:
	myTable();
	public slots:
		void addCustomRow();
};
# endif

Open in new window


This is the corresponding cpp file.
# include "myTable.h"
# include <QMessageBox>

myTable::myTable()
{
	this->setRowCount(0);
	this->setColumnCount(4);
};

void myTable::addCustomRow()
{
	int row = this->rowCount();
	this->insertRow(row);
	QMessageBox *q = new QMessageBox();
	q->setText("here");
	q->show();
	QComboBox *mBox = new QComboBox();
	mBox->show();
	mBox->addItem("Hello");
	mBox->addItem("World");
	mBox->show();
	QCheckBox *mCheck = new QCheckBox();
	mCheck->setChecked(false);
	mCheck->show();
	this->setCellWidget(row,0,mCheck);
	this->setCellWidget(row,1,mBox);
	this->item(row,2)->setText("Delta " + QString(row));
	this->item(row,3)->setText("Theta" + QString(row));
};

Open in new window

]
This is the myDIalog.h file:
# ifndef __MDLG__
# define __MDLG__
# include <QTableWidget>
# include <QPushButton>
# include <QComboBox>
# include <QDialog>
# include "myTable.h"

class myDialog : public QDialog
{
	Q_OBJECT
public:
	myTable * mTable;
	QPushButton * mAddButton;
	myDialog();
};
# endif

Open in new window


This is the myDialog .cpp file
# include "myDialog.h"
# include <QVBoxLayout>

myDialog::myDialog()
{
	this->mTable = new myTable();
	this->mTable->show();
	this->mAddButton = new QPushButton("Add Row");
	this->mAddButton->show();

	QVBoxLayout *mLay = new QVBoxLayout();
	
	mLay->addWidget(this->mAddButton);
	mLay->addWidget(this->mTable);
	this->connect(this->mAddButton,SIGNAL(clicked()),this->mTable,SLOT(this->mTable->addCustomRow()));
	this->setLayout(mLay);
	this->show();
};

Open in new window


ANd here is the corresponsing output of the above code."
 code output
The problem is that whenever I click on the "Add Row" button, the application crashes giving me the UI below saying that the Application "medisoft.exe has stopped working".:
 code output
I think the problem lies in the addCustomRow() function. I dont know what I am doing wrong.
PLease help.
runtimeerr.png
0
arjoshi77
Asked:
arjoshi77
  • 2
  • 2
1 Solution
 
evilrixSenior Software Engineer (Avast)Commented:
        this->item(row,2)->setText("Delta " + QString(row));
        this->item(row,3)->setText("Theta" + QString(row));
};

Open in new window


Just speculating but what if item() doesn't return a valid row (ie, there is no row 2 or 3)?
0
 
stachenovCommented:
> Just speculating but what if item() doesn't return a valid row (ie, there is no row 2 or 3)?

I also thought of that, but 2 and 3 are the column numbers, and "row" is the row number, which is returned by the "rowCount()". Considering that "insertRow(row)" is called right after that, "row" should be a valid row number.
0
 
stachenovCommented:
But it turns out it was close enough anyway.

The true reason is that there are no items 2 and 3 in the added row so item() returns NULL. The correct way to add items is this (taken right from the QTableWidget docs):
 
this->setItem(row, 2, new QTableWidgetItem("Delta " + QString::number(row)));
    this->setItem(row, 3, new QTableWidgetItem("Theta " + QString::number(row)));

Open in new window

Note that I also replaced QString(row) with QString::number(row) - that what you do if you need string representation of a number. QString(row) just doesn't make any sense as QString() doesn't even have a constructor with an int parameter. It is some sort of a miracle that it even compiles, but I think the reason is that the QString(QChar) constructor gets called with the integer converted to a QChar using QChar(int) constructor, which essentially yields a character with the specified code. That is, when you start adding rows into an empty table, you'll get characters with codes 0 (NUL), 1 (Ctrl+A), 2 (Ctrl+B), etc., which is obviously not what you want.
0
 
evilrixSenior Software Engineer (Avast)Commented:
I don't really know QT (hence saying my answer was speculative) but it was the only thing obvious in the posted code that would cause the described failure.
0
 
arjoshi77Author Commented:
Your solution worked . Thanks a lot.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now