1

I have to add a toolbar in Qt like the Windows file system explorer one under menu bar (I'm under Windows 7) , that means when the window width is reduced, icons which don't have enough place to be displayed are automatically hidden and put into a drop down list (which is displayed when clicking to an arrow which appears to the toolbar's right side). I first copy paste a code that I found to the web :

#include <QApplication>
#include <QAction>
#include <QMainWindow>
#include <QLineEdit>
#include <QToolBar>
#include <QHBoxLayout>



void initWindow(QMainWindow* w);


int main(int argc, char *argv[])
{
    Q_INIT_RESOURCE(application);

    QApplication app(argc, argv);

    QMainWindow mainWin;
    initWindow(&mainWin);
    mainWin.show();


    return app.exec();
}




void initWindow(QMainWindow* w)
{
    QLineEdit* searchBar = new QLineEdit;

    QAction* newAct = new QAction(QIcon(":/images/new.png"), "&New", w);
    newAct->setShortcuts(QKeySequence::New);

    QAction* openAct = new QAction(QIcon(":/images/open.png"), "&Open...", w);
    openAct->setShortcuts(QKeySequence::Open);

    QAction* saveAct = new QAction(QIcon(":/images/save.png"), "&Save", w);
    saveAct->setShortcuts(QKeySequence::Save);

    QAction* cutAct = new QAction(QIcon(":/images/cut.png"), "Cu&t", w);
    cutAct->setShortcuts(QKeySequence::Cut);

    QAction* copyAct = new QAction(QIcon(":/images/copy.png"), "&Copy", w);
    copyAct->setShortcuts(QKeySequence::Copy);

    QAction* pasteAct = new QAction(QIcon(":/images/paste.png"), "&Paste", w);
    pasteAct->setShortcuts(QKeySequence::Paste);


    QToolBar* fileToolBar = w->addToolBar("File");
    fileToolBar->addAction(newAct);
    fileToolBar->addAction(openAct);
    fileToolBar->addAction(saveAct);

    QToolBar* editToolBar = w->addToolBar("Edit");
    editToolBar->addAction(cutAct);
    editToolBar->addAction(copyAct);
    editToolBar->addAction(pasteAct);
    editToolBar->addWidget(searchBar);

}

... but the problem is that code works only for toolbars into a QMainWindow (and add by using QMainWindow::addToolbar() method). But into the code which I'm working for I have to do that into a QWidget, not a QWindow. So I created a horizontal layout, I added several widget into it (a QLineEdit and several QAction) and it works fine for QAction but not for QLineEdit : When I click to the arrow, all hidden QAction are visibles but not QLineEdit. Here is my code :

#include <QApplication>
#include <QtGui/QWindow>
#include <QToolbar>
#include <QVBoxLayout>
#include <QMainWindow>
#include <QPushButton>
#include <QAction>
#include <QIcon>
#include <QLineEdit>

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    QWidget* w = new QWidget;


    QHBoxLayout* tb1 = new QHBoxLayout;
    tb1->addWidget(new QPushButton("item11"));
    tb1->addWidget(new QPushButton("item12"));
    tb1->addWidget(new QPushButton("item13"));
    tb1->addWidget(new QPushButton("item14"));

    QHBoxLayout* spacerLayout = new QHBoxLayout;
    spacerLayout->addSpacerItem(new QSpacerItem(50, 20, QSizePolicy::MinimumExpanding,QSizePolicy::Fixed) );
    spacerLayout->setAlignment(Qt::AlignJustify);

    QWidget* sep = new QWidget;
    QRect rect = sep->geometry();
    rect.setWidth(0);
    sep->setGeometry(rect);
    QToolBar* tb3 = new QToolBar;
    QLineEdit* searchBar = new QLineEdit;
    QAction* item31 = new QAction(QIcon(":/images/cut.png"), "cut");
    QAction* item32 = new QAction(QIcon(":/images/copy.png"), "copy");
    QAction* item33 = new QAction(QIcon(":/images/open.png"), "open");
    QAction* item34 = new QAction(QIcon(":/images/paste.png"), "past");
    QAction* item35 = new QAction(QIcon(":/images/save.png"), "save");
    tb3->addWidget(sep);
    tb3->addWidget(searchBar);
    tb3->addAction(item31);
    tb3->addAction(item32);
    tb3->addAction(item33);
    tb3->addAction(item34);
    tb3->addAction(item35);

    QVBoxLayout* mainLayout = new QVBoxLayout;
    QHBoxLayout* topLayout = new QHBoxLayout;

    topLayout->addLayout(tb1);
    topLayout->addLayout(spacerLayout);
    topLayout->addWidget(tb3);


    QHBoxLayout* bottomLayout = new QHBoxLayout;
    bottomLayout->addWidget(new QPushButton);

    mainLayout->addLayout(topLayout);
    mainLayout->addLayout(bottomLayout);

    w->setLayout(mainLayout);
    w->show();

    return app.exec();
}

These are screenshots of the result with the 2nd solution : I first launch application :

http://img4.hostingpics.net/pics/224120tb1.jpg

When I reduce its width, widgets which are to the right side disapeared. Then I click to the arrow to display them into the drop down list and they are all displayed except the QLineEdit :

http://img4.hostingpics.net/pics/903380tb2.jpg

Is someone here knows what the problem is ? Thanks.

wperrad
  • 41
  • 4

2 Answers2

3

Regrettably, tool bars only function correctly when embedded in a QMainWindow. The good news is that you can use a QMainWindow as if it were a widget. You can parent it to another widget, and then it won't be a standalone window. I've done this, and it works well. I was creating the objects using Qt Designer, and I had to remove the QMainWindow menu bar because Designer creates that automatically.

It's not an intuitive thing to do, but it works just fine, and it's a fairly easy change. A well-written comment explaining why you did that would probably be welcomed by anyone else reading the code in the future...

goug
  • 2,294
  • 1
  • 11
  • 15
0

Thank you for your answer, I tried to test with a QMainWindow but it completely messed up the layout on which I worked and as it's a complex window (a lot of people worked on it in the past) and I have to finish my work soon I preferred to try a new approach. So after some research on the web I found that it's possible to do that I want even if the toolbar is not into a QMainWindow, but I have to replace all QWidget's that I want to have into QToolBar by a class which derived of QWidgetAction's, and instantiate them into QWidgetAction::createWidget() method. So I did this code which works correctly :

main.cpp :

#include <QApplication>
#include <QtGui/QWindow>
#include <QToolbar>
#include <QVBoxLayout>
#include <QMainWindow>
#include <QPushButton>
#include <QAction>
#include <QIcon>
#include <QLineEdit>
#include <QSlider>
#include <QVariant>
#include <QCheckBox>
#include <QWidgetAction>

#include "QMyWidgetAction.h"


void test2(QApplication& app);




int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    test2(app);
    return app.exec();
}

void test2(QApplication& app)
{

    QWidget* w = new QWidget;

    QHBoxLayout* l1 = new QHBoxLayout;
    l1->addWidget(new QPushButton("item11"));
    l1->addWidget(new QPushButton("item12"));
    l1->addWidget(new QPushButton("item13"));
    l1->addWidget(new QPushButton("item14"));

    QHBoxLayout* l2 = new QHBoxLayout;
    l2->addSpacerItem(new QSpacerItem(50, 20, QSizePolicy::MinimumExpanding,QSizePolicy::Fixed) );
    l2->setAlignment(Qt::AlignJustify);

    QHBoxLayout* l3 = new QHBoxLayout;
    QToolBar* tb = new QToolBar;
    l3->addWidget(tb);


    QAction* item31 = new QAction(QIcon(":/images/cut.png"), "cut");
    QAction* item32 = new QAction(QIcon(":/images/copy.png"), "copy");
    QAction* item33 = new QAction(QIcon(":/images/open.png"), "open");
    QAction* item34 = new QAction(QIcon(":/images/paste.png"), "past");
    QAction* item35 = new QAction(QIcon(":/images/save.png"), "save");
    QLineEdit* searchBar = new QLineEdit;
    QMyWidgetAction* widgetAction = new QMyWidgetAction(tb);
    QLineEditAction* lineEditAction = new QLineEditAction(tb);

    tb->addSeparator();
    tb->addWidget(searchBar);
    tb->addAction(item31);
    tb->addAction(item32);
    tb->addAction(item33);
    tb->addAction(item34);
    tb->addAction(item35);
    tb->addAction(widgetAction);
    tb->addAction(lineEditAction);

    QVBoxLayout* mainLayout = new QVBoxLayout;
    QHBoxLayout* topLayout = new QHBoxLayout;


    topLayout->addLayout(l1);
    topLayout->addLayout(l2);
    topLayout->addLayout(l3);


    QHBoxLayout* bottomLayout = new QHBoxLayout;
    bottomLayout->addWidget(new QPushButton);

    mainLayout->addLayout(topLayout);
    mainLayout->addLayout(bottomLayout);

    w->setLayout(mainLayout);
    w->show();

}

QMyWidgetAction.h :

#ifndef QMAYAWIDGETACTION_H
#define QMAYAWIDGETACTION_H

#include <QObject>
#include <QWidget>

#include <QWidgetAction>

class QLineEdit;

class QMyWidgetAction : public QWidgetAction
{
    Q_OBJECT
public:
    QMyWidgetAction(QWidget* parent);
    QWidget* createWidget(QWidget* parent);

};


class QLineEditAction : public QWidgetAction
{
    Q_OBJECT
public:
    QLineEditAction(QWidget* parent);
    QWidget* createWidget(QWidget* parent);

protected slots:
    virtual void    searchTextChanged(const QString& text);

private:
     QLineEdit* fWidget;

};

#endif // QMAYAWIDGETACTION_H

QMyWidgetAction.cpp :

#include <QApplication>
#include <QtGui/QWindow>
#include <QToolbar>
#include <QVBoxLayout>
#include <QMainWindow>
#include <QPushButton>
#include <QAction>
#include <QIcon>
#include <QLineEdit>
#include <QSlider>
#include <QVariant>
#include <QCheckBox>
#include <QWidgetAction>

#include "QMyWidgetAction.h"


QMyWidgetAction::QMyWidgetAction(QWidget* parent)
    : QWidgetAction(parent)
{

}


QWidget* QMyWidgetAction::createWidget(QWidget* parent)
{
    QPushButton* widget = new QPushButton("bouton", parent);
    widget->setMinimumSize(100, 30);
    return widget;
}


QLineEditAction::QLineEditAction(QWidget* parent)
    : QWidgetAction(parent)
{

}


QWidget* QLineEditAction::createWidget(QWidget* parent)
{
    fWidget = new QLineEdit(parent);
    connect(fWidget, SIGNAL(textChanged(QString)), this, SLOT(searchTextChanged(QString)));
    fWidget->setMinimumSize(100, 30);
    return fWidget;
}


void QLineEditAction::searchTextChanged(const QString& text)
{
    fWidget->setMinimumWidth(fWidget->minimumWidth() + 10);
}

So now here is what I get when I reduce the window width :

enter image description here

So the result is correct (and controls works, I tested them), but now I would like to know if it's possible to display the extension list horizontally instead of vertically ? (I mean "past" action at the right of "open" action, "save" action at the right of past action etc.) Thanks for your help.

wperrad
  • 41
  • 4