开发者

Qt Pb with QAction, QToolBar, QToolButton

开发者 https://www.devze.com 2023-03-01 18:56 出处:网络
I got a problem here. I\'m building a Qt app that uses slidingStackedWidgets in order to slide from one page to another one.

I got a problem here.

I'm building a Qt app that uses slidingStackedWidgets in order to slide from one page to another one.

I have 2 toolBars, a topToolBar and a bottomToolBar that do not slide when the central widget moves.

So I have declared a QAction in my mainWindow.h

 QAction *backToMainVert;

And in my mainWindow.cpp, when I call the second view by clicking a button, I call a slideInAdd() method that goes like this:

slidingStacked->slideInIdx(1); //this takes me back to my second page
backToMainVert = this->topToolBar->addWidget(backBarButton); 
//this adds a backButton to the topToolBar

Now the backBarButton is connected to a slideToMain() method which, when triggered takes me back to my first page and removes the backBarButton from the topToolBar

    this->topToolBar->removeAction(backToMainVert);
    slidingStacked->slideInIdx(0);

The first time I switch to the second page, no problem, my button is created and displayed on my topToolBar.

When I click the backBarButton, it goes back to the first page.

BUT the second time I want to return from the first page to the second page, the backBarButton never appears.

Uh, maybe this can help, here is the way I instantiate my backBarButton :

 backBarButton = new QToolButton(this);
 backBarButton->setText("Retour");
 backBarButton->setFixedSize(80,30);
 backBarButton->setStyleSheet("background-image: url(:/ToolBarButton.png);"
                             "background-repeat: no-repeat;"
                             "background-position: center center;"
                             "border:0;"
                             "color : white;"
                             "font-family: System;"
                             "font-style: bold;"
                             "font-size: 9pt;"
                             "color : white;");

Any idea what I'm missing here please ?

Thanks in advance, I'm stuck.

Miky Mike


Hello World,

Could someone suggest another way of achieving a ToolBar ? I wish I could find by myself but the debugger inside Qt doesn't give me much detail. It doesn't even stop on my breakpoint. Where should I start ?

Thanks for your help.

Mike


Ok guys,

开发者_如何学编程

Seems that the only way to go is to add a QAction to the topToolBar in the slideInAdd() method.

  void MainWindow::slideInAdd(){

     slidingStacked->setVerticalMode(true);
     slidingStacked->slideInIdx(1);
     backAction = new QAction(QIcon(":/ToolBarButton.png"), tr("&Retour"), this);
     topToolBar->addAction(backAction);
     connect(this->backAction,SIGNAL(triggered()),this, SLOT(slideInMainVert()));
 }

and this in the slideInMainVert() method :

    this->topToolBar->removeAction(backAction);

It's working that way but the problem is that I can't figure out how to customize the QToolButton that appears in the topToolBar. I would like it to be bigger than the default size (say...100x30) with some text on it (just like a button actually).

Could you help me please ?

Thanks a lot...

Mike


I have finally found a way !!!

Ready ?

1) So first we create a widget that will contain the layout that will hold the buttons :

QWidget * toolBarContainerWidget = new QWidget(this);

2) then we create the layout that will contain the buttons

QHBoxLayout *toolBarLayout = new QHBoxLayout(toolBarContainerWidget);

3) then we create the backButton :

QPushButton *testButton = new QPushButton("Go Back !",toolBarContainerWidget);
testButton->setFixedSize(80,20);
testButton->setStyleSheet("background-image: url(:/ToolBarButton.png);"
                          "background-repeat: no-repeat;"
                          "background-position: center center;"
                          "border:0;"
                          "color : white;"
                          "font-family: System;"
                          "font-style: bold;"
                          "font-size: 9pt;"
                          "color : white;");

4) We now add our testButton to our toolBarLayout :

toolBarLayout->addWidget(testButton);

5) We set the layout as the toolBarContainerWidget : (and here the brain begins to sweat...;-))

toolBarContainerWidget->setLayout(toolBarLayout);

6) We add this widget to our toolbar :

topToolBar->addWidget(toolBarContainerWidget);

7) We connect the Button to the slideInMainVert() method :

QObject::connect(testButton,SIGNAL(clicked()),this,SLOT(slideInMainVert()));

8) When we slide back to the first page, we use :

topToolBar->clear();

which clears up the toolBar...

Uh, well, I have to admit that it's a bit complicated but I haven't been able to find a better way. If you guys have any suggestion, please let me know...

Hope this can help anyway...

Miky Mike

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号