PDA

View Full Version : Need help with QToolButton



philipp1
27th October 2006, 13:37
Qt 3
I have some window which have a dock window.This dock window creates a QToolBar
mw->addToolBar(); mw it is a pointer to MainWindow.
This function creates ToolBar.
void MainWindow::addToolBar()
{
QToolBar* toolbar_but = new QToolBar("Open puctures",this, DockLeft , TRUE ,0);

}
It works.
When some event happens I must create button on toolbar.
I do it so:
mw->addButton();

void MainWindow::addButton()
{
QToolBar* toolbar_but;
btnpix = new QToolButton(toolbar_but, "");
}
One button it creates.
But when next event happens -> Segmentation fault .
What it could be?

munna
27th October 2006, 13:49
try




void MainWindow::addButton()
{
QToolBar* tb = new QToolButton(DockLeft, "");
}



BTW what is btnpix?

jpn
27th October 2006, 13:56
Store toolbar_but as a member variable. The toolbar_but in addButton() is different than in addToolBar(). It's just a dangling pointer in addButton().





void MainWindow::addToolBar()
{
// you should store toolbar_but as a member variable for later usage
QToolBar* toolbar_but = new QToolBar("Open puctures",this, DockLeft , TRUE ,0);
}

void MainWindow::addButton()
{
// the toolbar_but here is not the same toolbar_but as in addToolBar() !!
QToolBar* toolbar_but; // this is just a local variable, a dangling pointer pointing nowhere
btnpix = new QToolButton(toolbar_but, "");
}

philipp1
27th October 2006, 13:58
May be I dont understand something but how we can ti convert QToolButton to QToolBar?
For expierence I wrote your code: cannot convert 'QToolButton*' to 'QToolBar*' in initialization.:(

munna
27th October 2006, 14:00
oh sorry that was my mistake.

I actually meant




void MainWindow::addButton()
{
QToolButton* tb = new QToolButton(DockLeft, "");
}



Sorry once again.

munna
27th October 2006, 14:04
Store toolbar_but as a member variable. The toolbar_but in addButton() is different than in addToolBar(). It's just a dangling pointer in addButton().



One button it creates.

It's strange.

philipp1
27th October 2006, 14:46
oh sorry that was my mistake.

I actually meant




void MainWindow::addButton()
{
QToolButton* tb = new QToolButton(DockLeft, "");
}



Sorry once again.

They is no such constructor QToolButton(DockLeft, ""); (I did't find it)

munna
27th October 2006, 15:01
oh, I am sorry once again

It should be




void MainWindow::addButton()
{
QToolButton* tb = new QToolButton(this);
}



Actually I got confused because of the QToolBar consstructor that your are using.

The constructor that you are using is obsolete




void MainWindow::addToolBar()
{
QToolBar* toolbar_but = new QToolBar("Open puctures",this, DockLeft , TRUE ,0);// this is obsolete

}



you should rather be using




void MainWindow::addToolBar()
{
QToolBar* toolbar_but = new QToolBar(this,0);
toolbar_but->setLabel("Open Pictures");
}



or may be the other one according to your need.

philipp1
27th October 2006, 15:01
Store toolbar_but as a member variable. The toolbar_but in addButton() is different than in addToolBar(). It's just a dangling pointer in addButton().

I understand you. But how can I realise it? Toolbutton belongs to mainwindow and I call him in a dock window.

jpn
27th October 2006, 15:07
Sorry, I didn't understand. Realise what? You mean how can it be implemented?



// class declaration
class MainWindow : public ...
{
public:
...

private:
QToolBar* toolbar_but; // <--- a member variable
};




// class implementation
void MainWindow::addToolBar()
{
// store the toolbar as a member variable for later usage
toolbar_but = new QToolBar("Open puctures",this, DockLeft , TRUE ,0);
}

void MainWindow::addButton()
{
// use the member variable here
btnpix = new QToolButton(toolbar_but, "");
}

philipp1
27th October 2006, 15:24
I made as you say from the wery beginning.


class MainWindow : public QMainWindow {
Q_OBJECT

public:
....
QToolBar* toolbar_but;
void addToolBar();
void addButton();

mainwindow.cpp

void MainWindow::addButton()
{
//QToolBar* toolbar_but;
btnpix = new QToolButton(toolbar_but, "");
}

void MainWindow::addToolBar()
{
QToolBar* toolbar_but = new QToolBar("Open puctures",this, DockLeft , TRUE ,0);

jpn
27th October 2006, 15:29
I made as you say from the wery beginning.



void MainWindow::addToolBar()
{
// the problem is in here
QToolBar* toolbar_but = new QToolBar("Open puctures",this, DockLeft , TRUE ,0);
}



You are storing the toolbar to a local variable, not to the member variable. It should be:


void MainWindow::addToolBar()
{
// notice the missing type, you want to use the member variable, not to create a new local variable
toolbar_but = new QToolBar("Open puctures",this, DockLeft , TRUE ,0);
}

philipp1
27th October 2006, 15:37
You are storing the toolbar to a local variable, not to the member variable. It should be:


void MainWindow::addToolBar()
{
// notice the missing type, you want to use the member variable, not to create a new local variable
toolbar_but = new QToolBar("Open puctures",this, DockLeft , TRUE ,0);
}


Have a great thanks from me. Puting types was my mistake.
//(may be its friday)