2011-04-29 54 views
0

我在這裏遇到了問題。Qt Pb與QAction,QToolBar,QToolButton

我正在構建一個使用slidingStackedWidgets的Qt應用程序,以便從一個頁面滑動到另一個頁面。

我有2個工具欄,一個topToolBar和一個bottomToolBar,當中心控件移動時不會滑動。

所以我在mainWindow.h

QAction *backToMainVert; 

在我mainWindow.cpp宣佈的QAction,當我通過點擊按鈕調用第二個觀點,我稱之爲是這樣一個slideInAdd()方法這樣的:

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

現在backBarButton被連接到被觸發時把我帶回到我的第一頁,並刪除backBarButton從topToolBar

一個slideToMain()方法

我第一次切換到第二頁,沒問題,我的按鈕被創建並顯示在我的topToolBar上。

當我點擊backBarButton時,它會回到第一頁。

但是第二次我想從第一頁返回到第二頁,backBarButton從不出現。

呃,也許這可以幫助,這裏是我實例化我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;"); 

任何想法,我缺少在這裏嗎?

在此先感謝,我卡住了。

Miky邁克


的Hello World,

可能有人建議實現工具欄的另一種方式? 我希望自己能找到,但Qt 裏面的調試器並沒有給我太多的細節。它甚至不會停止在我的斷點上。 我應該從哪裏開始?

感謝您的幫助。

邁克


玉傢伙,

似乎要走的唯一方法是一個的QAction添加到topToolBar在slideInAdd()方法。

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())); 
} 

,這在slideInMainVert()方法:

this->topToolBar->removeAction(backAction); 

它的工作這種方式,但問題是,我無法弄清楚如何自定義出現在topToolBar的QToolButton。我希望它大於默認大小(比如說...100x30),上面有一些文字(就像一個按鈕一樣)。

請問您能幫我嗎?

非常感謝......

邁克

回答

2

我終於找到了一種方法!

準備好了嗎?

1)因此,我們首先創建一個小部件,將包含將持有的按鈕佈局:

QWidget * toolBarContainerWidget = new QWidget(this); 

2)然後,我們創建將包含按鈕

QHBoxLayout *toolBarLayout = new QHBoxLayout(toolBarContainerWidget); 

3佈局),那麼我們創建後退按鈕:

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)我們現在我們的testButton添加到我們的toolBarLayout:

toolBarLayout->addWidget(testButton); 

5)我們設置佈局爲toolBarContainerWidget:(這裏的大腦開始冒汗...... ;-))

toolBarContainerWidget->setLayout(toolBarLayout); 

6)我們這個小部件添加到我們的工具欄:

topToolBar->addWidget(toolBarContainerWidget); 

7)我們的按鈕連接到slideInMainVert()方法:

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

8)當我們滑回第一頁,我們使用:

topToolBar->clear(); 

這清除了工具欄...

呃,好吧,我不得不承認,這是一個有點複雜,但我一直沒能找到更好的辦法。 如果你們有任何建議,請讓我知道...

希望這可以幫助反正...

Miky邁克