QT slot function does not get called

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.
The problem is that the slot function addCustomRow() never gets called. I embedded test code ( See the MessageBox() call in the addCustomRow() slot definitiion. ). I never see that box when I run the executable///indicating that the code in addCUstomRow() never gets called. WHat am I doing wrong/missing?

// file 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

// file mytable.cpp
# 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

// file mydialog.h
# 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

// file myDialog.cpp
# 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

arjoshi77Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

arjoshi77Author Commented:
here is a screenshot of the UI that I see with the code above.
FIrst: No checkbox/combo/text populated from the addCustomRow() function is seen.
Second : Nothing happens when I click the AddRow button.
 UI of the code presented above.
0
Bill NolanOwner, Lead Technology ProgrammerCommented:
That is not a good test.  Have you used the debugger to see if it's called?
0
Bill NolanOwner, Lead Technology ProgrammerCommented:
Also, do you know if the "connect" function is doing what you want?
0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

irfan_omairCommented:
Here is code which solves your connection problem, it is independent example so you can compile and run separatedly, if you are not on windows then you remove #include debug\main.moc

Showing combo Box and other widget in any particular cell of table widget is done differently.
post new question about that
class myTable: public QTableWidget
{
 Q_OBJECT
public:
    myTable()
    {
        setRowCount(1);
        setColumnCount(4);
    }
public slots:
    void addmyRow()
    {
        insertRow(1);
        QMessageBox msgBox;
         msgBox.setText("The document has been modified.");
         msgBox.exec();
    }
};


class myDialog :public QDialog
{
    Q_OBJECT
public:
    myDialog()
    {
        QPushButton *addRow = new QPushButton("Add Rows");
        myTable *table = new myTable;
        QVBoxLayout *layout = new QVBoxLayout;
        setLayout(layout);
        layout->addWidget(addRow);
        layout->addWidget(table);

        connect(addRow,SIGNAL(clicked()),table,SLOT(addmyRow()));

    }

};

#include "debug\main.moc"
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    myDialog dlg;
    dlg.show();

    return a.exec();
}

Open in new window

0
stachenovCommented:
At least this line contains an error:
 
this->connect(this->mAddButton,SIGNAL(clicked()),this->mTable,SLOT(this->mTable->addCustomRow()));

Open in new window

You shouldn't pass anything but slot name and argument types to the SLOT macro, so the correct way is this:
 
connect(this->mAddButton, SIGNAL(clicked()),
              this->mTable, SLOT(addCustomRow()));

Open in new window

Errors such as this one is most easily debugged by running your application from the console in debug mode (maybe release will work as well, not sure). On Windows you have to compile it with CONFIG+=console in your project file. This way connect() will print a error message if something is wrong with the arguments (no such slot, invalid syntax, etc).
0
arjoshi77Author Commented:
stachenov:

I implemented the changes you suggested. But now I get an error message as shown in the image below.

runtimeerr.png
0
arjoshi77Author Commented:
The UI gets rendered fine. But the above runtime error happens once I click the "Add row" button as shown in the UI  of my first post.
0
stachenovCommented:
Try commenting out your code in the addCustomRow() method. Then add this code there:

QMessageBox::information(this, "addCustomRow()", "It really got called");

Open in new window

0
stachenovCommented:
If it works, then it means that your slot gets called all right. If your code for inserting a row doesn't work, it's a separate question.

Also, note that you're creating memory leaks using "new" with no "delete" each time the button is pressed.
0
arjoshi77Author Commented:
Dear stachenov:

I made the changes you suggested in the code as shown below.
void myTable::addCustomRow()
{
	int row = this->rowCount();
	this->insertRow(row);
	QMessageBox::information(this,"addCustomRow","It really got called");
	QComboBox *mBox = new QComboBox();	
	mBox->addItem("Hello");
	mBox->addItem("World");
	QCheckBox *mCheck = new QCheckBox();
	mCheck->setChecked(false);
	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));
	// delete added as per your advice. 
	delete mBox;
	delete mCheck;
};

Open in new window


THe problem is....the messagebox you asked me to implement shows up. But as soon as I hit the "OK" button , again the same UI saying the program is not working comes up. what am I doing wrong?
0
stachenovCommented:
Well, the slot gets called now, so the question is answered. Why your code crashes the application, it's another question and should be asked separately.

One thing to note that it makes even less sense to delete widgets immediately after they are created. Come to think of it, my advice about memory leaks was wrong, as setCellWidget() passes the ownership of the widget to the table, and then the table properly deletes it when needed, so you shouldn't be deleting it explicitly.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
arjoshi77Author Commented:
okay..i will put another post on the issue of crashnig.
thanks for your help.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.