?
Solved

QT slot function does not get called

Posted on 2011-10-20
12
Medium Priority
?
455 Views
Last Modified: 2012-06-21
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

0
Comment
Question by:arjoshi77
  • 5
  • 4
  • 2
  • +1
12 Comments
 

Author Comment

by:arjoshi77
ID: 37000988
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
 
LVL 7

Expert Comment

by:Bill Nolan
ID: 37001031
That is not a good test.  Have you used the debugger to see if it's called?
0
 
LVL 7

Expert Comment

by:Bill Nolan
ID: 37001063
Also, do you know if the "connect" function is doing what you want?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 7

Expert Comment

by:irfan_omair
ID: 37001755
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
 
LVL 4

Expert Comment

by:stachenov
ID: 37001805
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
 

Author Comment

by:arjoshi77
ID: 37010979
stachenov:

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

runtimeerr.png
0
 

Author Comment

by:arjoshi77
ID: 37010984
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
 
LVL 4

Expert Comment

by:stachenov
ID: 37010999
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
 
LVL 4

Expert Comment

by:stachenov
ID: 37011003
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
 

Author Comment

by:arjoshi77
ID: 37011252
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
 
LVL 4

Accepted Solution

by:
stachenov earned 2000 total points
ID: 37011286
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
 

Author Closing Comment

by:arjoshi77
ID: 37011398
okay..i will put another post on the issue of crashnig.
thanks for your help.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question