PDA

View Full Version : Add new TreeItemWidget using GUI of different class



sam123
7th June 2016, 11:45
Hello,
I want to add QTreeWidgetItem using QPussbutton and QlineEdit of different class. here is my code

mainWindow.h



#include <QMainWindow>
#include "name.h"
.
.
.
class MainWindow : public QMainWindow
{
Q_OBJECT

public:

void addNewItemToTree(QTreeWidgetItem *item);

private;

Ui::MainWindow *ui;

}


mainWindow.cpp



#include "mainwindow.h"
.
.
.
void MainWindow::addNewItemToTree(QTreeWidgetItem *item)
{
ui->treeWidget->setColumncount(1);
ui->treeWidget->addTopLevelItem(item);
}



name.h


#include <QWidget>
#include "mainwindow.h"
.
.
.
class Name : public QWidget
{
Q_OBJECT

public:

Qstring getName(void);

private:

void on_Pussbutton_clicked();

UI::Name * ui;



name.cpp



#include "name.h"
.
.
.
QString Name:: getName (void)
{
return ui->lineEdit->text();
}

void Name::on_PussButton_clicked()
{

QTreeWidgetItem *item = new QTreeWidgetItem();
item->setText(0,getName());


MainWindow * mainW; // use poiter of MainWindow class
mainW->addNewItemToTree(item); // add New Item in the fuction of MainWindow
}

(Code hab been typed so plese ignor smalls mistacks)
I am using these code. After clicking pussbutton item is not added in TreeWidget even I am not getting output either neither error.

looking for your guidance or solution..

Thanks..

Ginsengelf
7th June 2016, 15:38
Hi, I think you need to write
private slots: in line 14 of name.h so that the MOC can see which functions are slots.

Ginsengelf

edit: next time, please copy and paste the code, because something like


MainWindow * mainW; // use poiter of MainWindow class
mainW->addNewItemToTree(item); // add New Item in the fuction of MainWindow

will crash, and we don't know which errors are just "small mistakes" and which are actual code.

sam123
7th June 2016, 18:30
Thank you for your reply..

sorry, for mistakes and I have already written Private Slots: in name.h line number 14 still it doesnt work..

d_stranz
7th June 2016, 22:08
I have already written Private Slots: in name.h

The correct syntax is


private slots:

not


Private Slots:

And like the previous poster said, if you want help solving code problems, you must post your actual code, not something that sort of looks like your code but really isn't. How are we supposed to know what is a coding mistake versus what is a typing mistake?

Where is the connect() statement where you connect the QPushButton::clicked() signal to the on_PussButton_clicked() slot? If you did this in Qt Designer, is your button actually named "PussButton"?