PDA

View Full Version : toolbutton action is not happening.



phillip_Qt
18th November 2009, 06:31
Hi All
I've a tool button . i'm adding some action to that toolbutton. when i trigger that action i need some methode to b executed. i did like following.


header file
------------------
QAction *m_action;
actionlist();

source file
-----------------------
ui->toolButton->addAction(action);

constructor()
{
connect(m_action,SIGNAL( triggered()),
this, SLOT(claamethode()));
}
void actionlist()
{
m_action = new QAction(m_strTempName,ui->toolButton_3);
// assiging m_action here
ui->toolbutton->addaction(m_action);

}
But connect methode is not working. how to solve this?

Lykurg
18th November 2009, 07:26
Are you sure that m_action is not null when you establish the connection?

yogeshgokul
18th November 2009, 07:28
Hi All
I've a tool button . i'm adding some action to that toolbutton. when i trigger that action i need some methode to b executed. i did like following.


header file
------------------
QAction *m_action;
actionlist();

source file
-----------------------
ui->toolButton->addAction(action);

constructor()
{
connect(m_action,SIGNAL( triggered()),
this, SLOT(claamethode()));
}
void actionlist()
{
m_action = new QAction(m_strTempName,ui->toolButton_3);
// assiging m_action here
ui->toolbutton->addaction(m_action);

}
But connect methode is not working. how to solve this?

What is this :confused::confused:

phillip_Qt
18th November 2009, 08:01
What is this :confused::confused:
Hi Yogesh. Below is the complete code.


Header file
------------------

class MainWindow : public QMainWindow
{
Q_OBJECT

public:
MainWindow(QWidget *parent = 0);
~MainWindow();

private:
Ui::MainWindow *ui;


private:
//! To collect the list of classrooms and computers added to the coressponding classroom
QList<QTreeWidgetItem *> *m_ClassRoom;
QList<QTreeWidgetItem *> m_computersAddedToCalssroom;
QStringList m_strlistClassRoom;
QTreeWidgetItem *m_treelist;
QList<QTreeWidgetItem *> temp;


private slots:
//! To add Computers
void on_toolButton_3_clicked();
void addComputers();
//! To add classroom
void addClassRoom();

private slots:
//! To handle the right click event in The right side bar
void ContextMenuDisplayMethod(const QPoint &);

private:
QMenu contextMenu;

private:
//! treewidget context menu
QAction * m_actionClassroom;
QAction * m_actionComputer;
QList<QAction *> m_actionsList;

private:
//SingleClass SingleClassobj;
QString m_strIpEntered;
QString m_strClassRoomSel;
QString m_strUserNameEnter;
clientSettingsDialog m_clientSettingsDlgobj;

//! To add sub Items in tree widget
QModelIndex m_modelIndex;


//! To add sub Items in tree widget
QTreeWidgetItem *m_Childlist ;
int index;

//! Menu showing the list of added classroom from classroom button
QMenu m_classRoomListMenu;
QStringList m_strlistMenu;
QString m_strTempName;

private slots:
void openClassInClientArea();

private:
void test();
QAction *acttest;
QMenu menu;
};

#endif // MAINWINDOW_H


source file
-------------------

SingleClass SingleClassobj;


MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent), ui(new Ui::MainWindow)
{
ui->setupUi(this);
QWidget::showMaximized();

m_ClassRoom = new QList<QTreeWidgetItem *>;

ui->treeWidget->setContextMenuPolicy(Qt::CustomContextMenu);
ui->treeWidget->setColumnCount(2);
ui->treeWidget->setColumnWidth(0,200);
QStringList columns;
columns << tr( "Classrooms/computers" ) << tr( "IP-address" );
ui->treeWidget->setHeaderLabels(columns);
connect(ui->treeWidget, SIGNAL(customContextMenuRequested(const QPoint &)),
this, SLOT(ContextMenuDisplayMethod(const QPoint &)));
//!
m_actionClassroom = new QAction(tr("Add ClassRoom"),ui->treeWidget);
m_actionComputer = new QAction(tr("Add Computer"),ui->treeWidget);

//! For context menu display methode in treewidget
connect(m_actionClassroom, SIGNAL(triggered()), this, SLOT(addClassRoom()));
connect(m_actionComputer, SIGNAL(triggered()),this, SLOT(addComputers()));

contextMenu.addAction(m_actionClassroom);
contextMenu.addAction(m_actionComputer);

//! To hide client setting dialog
m_clientSettingsDlgobj.hide();

acttest = new QAction(m_strTempName,ui->toolButton_3);

}

MainWindow::~MainWindow()
{
delete ui;
}

void MainWindow::addComputers()
{
if(m_ClassRoom->size() == 0)
{
if( QMessageBox::question( window(), tr( "Missing classroom" ),
tr( "Before adding computers "
"you have to "
"create at least one "
"classroom.\nDo you "
"want to create a new "
"classrom now?" ),
QMessageBox::Yes,
QMessageBox::No ) ==
QMessageBox::No )
{
return;
}
addClassRoom();
}
else
{
if(m_clientSettingsDlgobj.exec())
{
m_strIpEntered = m_clientSettingsDlgobj.getIP();;
m_strClassRoomSel = m_clientSettingsDlgobj.getSelClassRoom();
m_strUserNameEnter = m_clientSettingsDlgobj.getUserName();
if(m_strUserNameEnter.isEmpty())
{
m_strUserNameEnter = m_strIpEntered;
}
temp = ui->treeWidget->findItems(m_strClassRoomSel , Qt::MatchExactly);
QTreeWidgetItemIterator it(ui->treeWidget);

for(int i = 0; i < ui->treeWidget->topLevelItemCount(); i++)
{

QTreeWidgetItem *parent = ui->treeWidget->topLevelItem(i);
if(parent->text(0) == m_strClassRoomSel)
{
m_Childlist = new QTreeWidgetItem(parent);
m_Childlist->setText(0, m_strUserNameEnter);
m_Childlist->setText(1, m_strIpEntered);

return;
}
}
}

}
}

void MainWindow::addClassRoom()
{
bool bOk = false;
QString classroom_name = QInputDialog::getText( this,
tr( "New classroom" ),
tr( "Please enter the name of the classroom you "
"want to create." ),
QLineEdit::Normal, tr( "New classroom" ), &bOk );

if(!classroom_name.isEmpty())
{
bool bTemp = false;
for(int i = 0; i < m_strlistClassRoom.size(); i++)
{
if(m_strlistClassRoom.at(i) == classroom_name)
bTemp = true;
}
if(bTemp == false)
{
m_strlistClassRoom.append(classroom_name);
m_treelist = new QTreeWidgetItem;
m_treelist->setText(0,classroom_name);
m_ClassRoom->append(m_treelist);

ui->treeWidget->addTopLevelItem(m_treelist);

SingleClassobj.setSingleClass(classroom_name);
}

}

for(int i = 0; i <SingleClassobj.getSingleCLass().size() ; i++)
{
m_clientSettingsDlgobj.setClassComboBox(SingleClas sobj.getSingleCLass().at(i));

}

//! To set menu in classroom toolButton
test();

}


void MainWindow::ContextMenuDisplayMethod(const QPoint &pt)
{
contextMenu.popup(QCursor::pos());
contextMenu.exec(QCursor::pos());
}

void MainWindow::on_toolButton_3_clicked()
{

qDebug()<<"INside toolButton";
ui->toolButton_3->showMenu();
menu.exec();

}

void MainWindow::openClassInClientArea()
{
// QAction * act = m_classRoomListMenu.activeAction();
QAction * act = ui->toolButton_3->defaultAction();

QString strClassroom = act->text();

qDebug()<<"Class rRoom : "<<strClassroom;
//QList<QTreeWidget> treewidgetchildren = ui->treeWidget->findChildren<QTreeWidget>(strClassroom);

}

void MainWindow::test()
{
bool bTemp = false;

for(int i = 0; i < m_strlistClassRoom.size(); i++)
{
bTemp = false;
m_strTempName = ("ClassRoom ")+ m_strlistClassRoom.at(i);
acttest = new QAction(m_strTempName,ui->toolButton_3);
for(int j = 0; j < m_strlistMenu.size(); j++)
{
if(m_strlistMenu.at(j) == m_strTempName)
{
bTemp = true;
}

}
if(bTemp == false)
{
acttest->setText(m_strTempName);
m_strlistMenu.append(m_strTempName);
//m_classRoomListMenu.addAction(m_strTempName);
qDebug()<<"MainWindow::test() : acttest value : "<<acttest->text();
ui->toolButton_3->addAction(acttest);
}
}
connect(acttest, SIGNAL(triggered()),
this, SLOT(openClassInClientArea()));

}

Now i'm able to trigger the action. but how to get, which action is triggered. i mean in open openClassInClientArea() how ll i know that which action is triggered.

Lykurg
18th November 2009, 08:09
the connect must inside the loop!
to find what button/action was clicked use a QSignalMapper or QObject::sender().

phillip_Qt
18th November 2009, 08:29
the connect must inside the loop!
to find what button/action was clicked use a QSignalMapper or QObject::sender().


Thank u Lykurg for quick reply.

the connect must inside the loop!

do u mean like

void MainWindow::test()
{
bool bTemp = false;

for(int i = 0; i < m_strlistClassRoom.size(); i++)
{
bTemp = false;
m_strTempName = ("ClassRoom ")+ m_strlistClassRoom.at(i);
acttest = new QAction(m_strTempName,ui->toolButton_3);
for(int j = 0; j < m_strlistMenu.size(); j++)
{
if(m_strlistMenu.at(j) == m_strTempName)
{
bTemp = true;
}
}
if(bTemp == false)
{
acttest->setText(m_strTempName);
m_strlistMenu.append(m_strTempName);
//m_classRoomListMenu.addAction(m_strTempName);
qDebug()<<"MainWindow::test() : acttest value : "<<acttest->text();
ui->toolButton_3->addAction(acttest);
}
connect(acttest, SIGNAL(triggered()),
this, SLOT(openClassInClientArea()));
}
}

and also

void MainWindow::openClassInClientArea()
{
QString strClassroom = acttest->sender()->objectName();
}
is giving error as objectname() is private methode of QObject. :( Can u plz suggest me how to do?

Lykurg
18th November 2009, 08:52
Can u plz suggest me how to do?

Yes, read the docs to QObject::sender();)

Better would be using a QSignalMapper, but if you want use the sender() see what is the output of
void MainWindow::openClassInClientArea()
{
qWarning() << QObject::sender();
}
when you click the different buttons.

phillip_Qt
18th November 2009, 09:04
Yes, read the docs to QObject::sender();)

Better would be using a QSignalMapper, but if you want use the sender() see what is the output of
void MainWindow::openClassInClientArea()
{
qWarning() << QObject::sender();
}
when you click the different buttons.
Thank u very much
I put following inside the loop

connect(acttest, SIGNAL(triggered()),
this, SLOT(openClassInClientArea()));
strClassroom = acttest->text();

and its workig fine.
Thank u very much.

phillip_Qt
18th November 2009, 12:16
Yes, read the docs to QObject::sender();)

Better would be using a QSignalMapper, but if you want use the sender() see what is the output of
void MainWindow::openClassInClientArea()
{
qWarning() << QObject::sender();
}
when you click the different buttons.

Hi
m_classroomActionList->sender()->objectName(); is giving error.
error: `QObject* QObject::sender() const' is protected.:(

Lykurg
18th November 2009, 12:33
Hi
m_classroomActionList->sender()->objectName(); is giving error.
error: `QObject* QObject::sender() const' is protected.:(

Because you have to use
QObject::sender();//or
sender();

and NOT
m_classroomActionList->sender();
!!! Like I wrote in the small example!

phillip_Qt
18th November 2009, 12:37
Because you have to use
QObject::sender();//or
sender();

and NOT
m_classroomActionList->sender();
!!! Like I wrote in the small example!

But

strClassroom = QObject::sender()->objectName();
is returning a null string . :(

phillip_Qt
19th November 2009, 04:57
But

strClassroom = QObject::sender()->objectName();
is returning a null string . :(

I'm stuck with the problem. Plz help me. :(

Lykurg
19th November 2009, 08:49
maybe it's because your sender object has no name! QObject::sender()->objectName() works perfect for me. And by the way, you don't need that name:
if (QObject::sender() == ui->button1)
//...

But that is not a good style, so better use QSignalMapper...

phillip_Qt
19th November 2009, 09:13
maybe it's because your sender object has no name! QObject::sender()->objectName() works perfect for me. And by the way, you don't need that name:
if (QObject::sender() == ui->button1)
//...

But that is not a good style, so better use QSignalMapper...

Thank u Lykurg.
i did like following


QSignalMapper * signalMapper;// data member and allocate memory in c'tor

connect(m_classroomActionList, SIGNAL(triggered()),
this, SLOT(openClassInClientArea()));

connect(m_classroomActionList, SIGNAL(triggeredd()),
signalMapper, SLOT(map()));

connect(signalMapper, SIGNAL(mapped(const QString &)),
this, SIGNAL(clicked(const QString &)));

but signalMapper->objectName(); is returning null. can u plz tell me whats the mistake in my code.:( i'm totally stuck with this problem since last 2 days.