Vladimir Prus wrote:

> As another try, using
> 
>   toolbar->boxLayout()->addStrech()
> 
> fails to work in two ways:
> 
>   - stretch is added *before* buttons
>   - the toolbar is not resized to full width.
> 
> The thing I hate about Qt is that sometimes things just don't work and
> it's extra hard to figure out what to do.

Sorry for flooding this list, but I realized I've made an implementation bug
in one of approached I've tried. I've tried to derive from QToolButton and
override sizeHint(), but forgot "const" after the method, so I did not
override anything. With const, things look better.

Take a look at:

  http://zigzag.cs.msu.su/~ghost/toolbar.png

The window above is result of your approach. The window below is the same
but with overrided sizeHint. Looks like extra space is gone.

I attach the complete source, maybe that will help.

- Volodya

P.S. Now back to writing 'sizeHint()' method in my own code, with "const".

#include "toolbar.h"

#include <qapplication.h>
#include <qpixmap.h>
#include <qtoolbar.h>
#include <qtoolbutton.h>
#include <qlayout.h>

#include <cassert>
#include <iostream>
#include <sstream>


namespace {

char const * cdtoparent_xpm[]={
    "15 13 3 1",
    ". c None",
    "* c #000000",
    "a c #ffff99",
    "..*****........",
    ".*aaaaa*.......",
    "***************",
    "*aaaaaaaaaaaaa*",
    "*aaaa*aaaaaaaa*",
    "*aaa***aaaaaaa*",
    "*aa*****aaaaaa*",
    "*aaaa*aaaaaaaa*",
    "*aaaa*aaaaaaaa*",
    "*aaaa******aaa*",
    "*aaaaaaaaaaaaa*",
    "*aaaaaaaaaaaaa*",
    "***************"};


class My_tb : public QToolButton
{
public:
    My_tb(QWidget* parent) : QToolButton(parent) {}
    
    QSize sizeHint() const { return QSize(0, 0); }
    QSize minimumSizeHint() const { return QSize(0, 0); }
};

QToolBar * build_toolbar(QMainWindow * parent)
{
	std::ostringstream ss;
	static int count = 1;
	ss << "toolbar" << count++;
	char const * const toolbar_name = ss.str().c_str();

	QToolBar * toolbar = new QToolBar(toolbar_name, parent, parent);
	QToolButton * tb =
		new QToolButton(QPixmap(cdtoparent_xpm), "One directory up",
				QString::null, parent, SLOT(cdUp()),
				toolbar, "cd up");
				
//	tb->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);				
				
//	toolbar->addSeparator();
//	toolbar->

//	toolbar->setHorizontalStretchable(true);
//	toolbar->setVerticalStretchable(true);
//	QWidget* space = new QWidget(toolbar);
//	space->resize(0, 0);
//	space->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
	//toolbar->setStretchableWidget(tb);
	
//	QBoxLayout* b = toolbar->boxLayout();
//        b->addStretch(0);
    //	b->addSpacing(100);
//	b->insertSpacing(-1, 100);
	
#if 1
	tb = new QToolButton(toolbar, "");
//	tb = new My_tb(toolbar);
	tb->setEnabled(false);
	toolbar->setStretchableWidget(tb);
#endif

	return toolbar;
}

} // namespace anon


MyWindow::MyWindow()
{
	resize(600,300);

	QToolBar * toolbar1 = build_toolbar(this);
	QToolBar * toolbar2 = build_toolbar(this);

	toolBars(Qt::DockTop).append(toolbar1);
	toolBars(Qt::DockTop).append(toolbar2);
}


void MyWindow::cdUp()
{
	std::cout << "cd up" << std::endl;
}


#include "toolbar.moc"



int main(int argc, char * argv[])
{
	QApplication qapp(argc, argv);
	MyWindow * my_window = new MyWindow;
	qapp.setMainWidget(my_window);

	my_window->show();
	return qapp.exec();
}

Reply via email to