How to add QSlider to toolbar in mainwindow using pyqt5 in python









up vote
1
down vote

favorite












I'm now using PyQt5 in python to develop a GUI for my program. I have added some QAction to the toolbar, like start, pause and stop icon. However, I don't know how to add a QSlider to the toolbar in my mainwindow. I prefer to add the QSlider to the position which is marked by a red rectangle. Thanks!



enter image description here










share|improve this question

























    up vote
    1
    down vote

    favorite












    I'm now using PyQt5 in python to develop a GUI for my program. I have added some QAction to the toolbar, like start, pause and stop icon. However, I don't know how to add a QSlider to the toolbar in my mainwindow. I prefer to add the QSlider to the position which is marked by a red rectangle. Thanks!



    enter image description here










    share|improve this question























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      I'm now using PyQt5 in python to develop a GUI for my program. I have added some QAction to the toolbar, like start, pause and stop icon. However, I don't know how to add a QSlider to the toolbar in my mainwindow. I prefer to add the QSlider to the position which is marked by a red rectangle. Thanks!



      enter image description here










      share|improve this question













      I'm now using PyQt5 in python to develop a GUI for my program. I have added some QAction to the toolbar, like start, pause and stop icon. However, I don't know how to add a QSlider to the toolbar in my mainwindow. I prefer to add the QSlider to the position which is marked by a red rectangle. Thanks!



      enter image description here







      python pyqt5






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 11 at 1:28









      Jiawei Lu

      506




      506






















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          1
          down vote



          accepted










          QToolBar has the addWidget() method that allows you to add widgets:



          from PyQt5 import QtCore, QtWidgets

          class MainWindow(QtWidgets.QMainWindow):
          def __init__(self, parent=None):
          super(MainWindow, self).__init__(parent)
          toolbar = self.addToolBar("toolbar")
          toolbar.addAction("start")
          toolbar.addAction("pause")
          toolbar.addAction("stop")
          self.slider = QtWidgets.QSlider(QtCore.Qt.Horizontal)
          toolbar.addWidget(self.slider)

          if __name__ == '__main__':
          import sys
          app = QtWidgets.QApplication(sys.argv)
          w = MainWindow()
          w.show()
          sys.exit(app.exec_())


          enter image description here






          share|improve this answer




















          • cool! It does work for me. But how can I adjust the size of this slider? I mean it's too long for me. I have tried toolbar.setGeometry function, but it doesn't work. Another question, do you know what does the function toolbar.SetFoucusPolicy do? Thanks for your kind help
            – Jiawei Lu
            Nov 11 at 5:21










          • @JiaweiLu if you want to set a fixed width you can use setFixedWitdh(), for example self.slider.setFixedWidth(100), On the other hand, SetFocusPolicy establishes the policy of how the focus gets a widget, for example by pressing the widget, with the TAB key, etc. read doc.qt.io/qt-5/qwidget.html#focusPolicy-prop. On the other hand if my answer does not forget to mark it as correct, if you do not know how to do it then review the tour, that is the best way to thank.
            – eyllanesc
            Nov 11 at 5:27










          • A question arises, could we use alignment such as toolbar.addWidget(self.slider, alignment=QtCore.Qt.AlignCenter). I do not think so that would work simply. Could this slider aligns to left or center?
            – Zar Kha
            Nov 12 at 9:38










          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













           

          draft saved


          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53245066%2fhow-to-add-qslider-to-toolbar-in-mainwindow-using-pyqt5-in-python%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          1
          down vote



          accepted










          QToolBar has the addWidget() method that allows you to add widgets:



          from PyQt5 import QtCore, QtWidgets

          class MainWindow(QtWidgets.QMainWindow):
          def __init__(self, parent=None):
          super(MainWindow, self).__init__(parent)
          toolbar = self.addToolBar("toolbar")
          toolbar.addAction("start")
          toolbar.addAction("pause")
          toolbar.addAction("stop")
          self.slider = QtWidgets.QSlider(QtCore.Qt.Horizontal)
          toolbar.addWidget(self.slider)

          if __name__ == '__main__':
          import sys
          app = QtWidgets.QApplication(sys.argv)
          w = MainWindow()
          w.show()
          sys.exit(app.exec_())


          enter image description here






          share|improve this answer




















          • cool! It does work for me. But how can I adjust the size of this slider? I mean it's too long for me. I have tried toolbar.setGeometry function, but it doesn't work. Another question, do you know what does the function toolbar.SetFoucusPolicy do? Thanks for your kind help
            – Jiawei Lu
            Nov 11 at 5:21










          • @JiaweiLu if you want to set a fixed width you can use setFixedWitdh(), for example self.slider.setFixedWidth(100), On the other hand, SetFocusPolicy establishes the policy of how the focus gets a widget, for example by pressing the widget, with the TAB key, etc. read doc.qt.io/qt-5/qwidget.html#focusPolicy-prop. On the other hand if my answer does not forget to mark it as correct, if you do not know how to do it then review the tour, that is the best way to thank.
            – eyllanesc
            Nov 11 at 5:27










          • A question arises, could we use alignment such as toolbar.addWidget(self.slider, alignment=QtCore.Qt.AlignCenter). I do not think so that would work simply. Could this slider aligns to left or center?
            – Zar Kha
            Nov 12 at 9:38














          up vote
          1
          down vote



          accepted










          QToolBar has the addWidget() method that allows you to add widgets:



          from PyQt5 import QtCore, QtWidgets

          class MainWindow(QtWidgets.QMainWindow):
          def __init__(self, parent=None):
          super(MainWindow, self).__init__(parent)
          toolbar = self.addToolBar("toolbar")
          toolbar.addAction("start")
          toolbar.addAction("pause")
          toolbar.addAction("stop")
          self.slider = QtWidgets.QSlider(QtCore.Qt.Horizontal)
          toolbar.addWidget(self.slider)

          if __name__ == '__main__':
          import sys
          app = QtWidgets.QApplication(sys.argv)
          w = MainWindow()
          w.show()
          sys.exit(app.exec_())


          enter image description here






          share|improve this answer




















          • cool! It does work for me. But how can I adjust the size of this slider? I mean it's too long for me. I have tried toolbar.setGeometry function, but it doesn't work. Another question, do you know what does the function toolbar.SetFoucusPolicy do? Thanks for your kind help
            – Jiawei Lu
            Nov 11 at 5:21










          • @JiaweiLu if you want to set a fixed width you can use setFixedWitdh(), for example self.slider.setFixedWidth(100), On the other hand, SetFocusPolicy establishes the policy of how the focus gets a widget, for example by pressing the widget, with the TAB key, etc. read doc.qt.io/qt-5/qwidget.html#focusPolicy-prop. On the other hand if my answer does not forget to mark it as correct, if you do not know how to do it then review the tour, that is the best way to thank.
            – eyllanesc
            Nov 11 at 5:27










          • A question arises, could we use alignment such as toolbar.addWidget(self.slider, alignment=QtCore.Qt.AlignCenter). I do not think so that would work simply. Could this slider aligns to left or center?
            – Zar Kha
            Nov 12 at 9:38












          up vote
          1
          down vote



          accepted







          up vote
          1
          down vote



          accepted






          QToolBar has the addWidget() method that allows you to add widgets:



          from PyQt5 import QtCore, QtWidgets

          class MainWindow(QtWidgets.QMainWindow):
          def __init__(self, parent=None):
          super(MainWindow, self).__init__(parent)
          toolbar = self.addToolBar("toolbar")
          toolbar.addAction("start")
          toolbar.addAction("pause")
          toolbar.addAction("stop")
          self.slider = QtWidgets.QSlider(QtCore.Qt.Horizontal)
          toolbar.addWidget(self.slider)

          if __name__ == '__main__':
          import sys
          app = QtWidgets.QApplication(sys.argv)
          w = MainWindow()
          w.show()
          sys.exit(app.exec_())


          enter image description here






          share|improve this answer












          QToolBar has the addWidget() method that allows you to add widgets:



          from PyQt5 import QtCore, QtWidgets

          class MainWindow(QtWidgets.QMainWindow):
          def __init__(self, parent=None):
          super(MainWindow, self).__init__(parent)
          toolbar = self.addToolBar("toolbar")
          toolbar.addAction("start")
          toolbar.addAction("pause")
          toolbar.addAction("stop")
          self.slider = QtWidgets.QSlider(QtCore.Qt.Horizontal)
          toolbar.addWidget(self.slider)

          if __name__ == '__main__':
          import sys
          app = QtWidgets.QApplication(sys.argv)
          w = MainWindow()
          w.show()
          sys.exit(app.exec_())


          enter image description here







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 11 at 1:41









          eyllanesc

          69k93052




          69k93052











          • cool! It does work for me. But how can I adjust the size of this slider? I mean it's too long for me. I have tried toolbar.setGeometry function, but it doesn't work. Another question, do you know what does the function toolbar.SetFoucusPolicy do? Thanks for your kind help
            – Jiawei Lu
            Nov 11 at 5:21










          • @JiaweiLu if you want to set a fixed width you can use setFixedWitdh(), for example self.slider.setFixedWidth(100), On the other hand, SetFocusPolicy establishes the policy of how the focus gets a widget, for example by pressing the widget, with the TAB key, etc. read doc.qt.io/qt-5/qwidget.html#focusPolicy-prop. On the other hand if my answer does not forget to mark it as correct, if you do not know how to do it then review the tour, that is the best way to thank.
            – eyllanesc
            Nov 11 at 5:27










          • A question arises, could we use alignment such as toolbar.addWidget(self.slider, alignment=QtCore.Qt.AlignCenter). I do not think so that would work simply. Could this slider aligns to left or center?
            – Zar Kha
            Nov 12 at 9:38
















          • cool! It does work for me. But how can I adjust the size of this slider? I mean it's too long for me. I have tried toolbar.setGeometry function, but it doesn't work. Another question, do you know what does the function toolbar.SetFoucusPolicy do? Thanks for your kind help
            – Jiawei Lu
            Nov 11 at 5:21










          • @JiaweiLu if you want to set a fixed width you can use setFixedWitdh(), for example self.slider.setFixedWidth(100), On the other hand, SetFocusPolicy establishes the policy of how the focus gets a widget, for example by pressing the widget, with the TAB key, etc. read doc.qt.io/qt-5/qwidget.html#focusPolicy-prop. On the other hand if my answer does not forget to mark it as correct, if you do not know how to do it then review the tour, that is the best way to thank.
            – eyllanesc
            Nov 11 at 5:27










          • A question arises, could we use alignment such as toolbar.addWidget(self.slider, alignment=QtCore.Qt.AlignCenter). I do not think so that would work simply. Could this slider aligns to left or center?
            – Zar Kha
            Nov 12 at 9:38















          cool! It does work for me. But how can I adjust the size of this slider? I mean it's too long for me. I have tried toolbar.setGeometry function, but it doesn't work. Another question, do you know what does the function toolbar.SetFoucusPolicy do? Thanks for your kind help
          – Jiawei Lu
          Nov 11 at 5:21




          cool! It does work for me. But how can I adjust the size of this slider? I mean it's too long for me. I have tried toolbar.setGeometry function, but it doesn't work. Another question, do you know what does the function toolbar.SetFoucusPolicy do? Thanks for your kind help
          – Jiawei Lu
          Nov 11 at 5:21












          @JiaweiLu if you want to set a fixed width you can use setFixedWitdh(), for example self.slider.setFixedWidth(100), On the other hand, SetFocusPolicy establishes the policy of how the focus gets a widget, for example by pressing the widget, with the TAB key, etc. read doc.qt.io/qt-5/qwidget.html#focusPolicy-prop. On the other hand if my answer does not forget to mark it as correct, if you do not know how to do it then review the tour, that is the best way to thank.
          – eyllanesc
          Nov 11 at 5:27




          @JiaweiLu if you want to set a fixed width you can use setFixedWitdh(), for example self.slider.setFixedWidth(100), On the other hand, SetFocusPolicy establishes the policy of how the focus gets a widget, for example by pressing the widget, with the TAB key, etc. read doc.qt.io/qt-5/qwidget.html#focusPolicy-prop. On the other hand if my answer does not forget to mark it as correct, if you do not know how to do it then review the tour, that is the best way to thank.
          – eyllanesc
          Nov 11 at 5:27












          A question arises, could we use alignment such as toolbar.addWidget(self.slider, alignment=QtCore.Qt.AlignCenter). I do not think so that would work simply. Could this slider aligns to left or center?
          – Zar Kha
          Nov 12 at 9:38




          A question arises, could we use alignment such as toolbar.addWidget(self.slider, alignment=QtCore.Qt.AlignCenter). I do not think so that would work simply. Could this slider aligns to left or center?
          – Zar Kha
          Nov 12 at 9:38

















           

          draft saved


          draft discarded















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53245066%2fhow-to-add-qslider-to-toolbar-in-mainwindow-using-pyqt5-in-python%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Top Tejano songwriter Luis Silva dead of heart attack at 64

          天津地下鉄3号線

          How to fill missing numeric if any value in a subset is missing, all other columns with the same subset are missing