How do you set the column width on a QTreeView?











up vote
12
down vote

favorite
1












Bear with me, I'm still new to QT and am having trouble wrapping my brain around how it does things.



I've created and populated a QTreeView with two columns:



class AppForm(QMainWindow):
def __init__(self, parent = None):
super(AppForm, self).__init__(parent)
self.model = QStandardItemModel()
self.view = QTreeView()
self.view.setColumnWidth(0, 800)
self.view.setEditTriggers(QAbstractItemView.NoEditTriggers)
self.view.setModel(self.model)
self.setCentralWidget(self.view)


Everything's working great, except the columns are extremely narrow. I hoped that setColumnWidth(0, 800) would widen the first column, but it doesn't seem to be having any effect. What's the proper method for setting column widths?










share|improve this question




























    up vote
    12
    down vote

    favorite
    1












    Bear with me, I'm still new to QT and am having trouble wrapping my brain around how it does things.



    I've created and populated a QTreeView with two columns:



    class AppForm(QMainWindow):
    def __init__(self, parent = None):
    super(AppForm, self).__init__(parent)
    self.model = QStandardItemModel()
    self.view = QTreeView()
    self.view.setColumnWidth(0, 800)
    self.view.setEditTriggers(QAbstractItemView.NoEditTriggers)
    self.view.setModel(self.model)
    self.setCentralWidget(self.view)


    Everything's working great, except the columns are extremely narrow. I hoped that setColumnWidth(0, 800) would widen the first column, but it doesn't seem to be having any effect. What's the proper method for setting column widths?










    share|improve this question


























      up vote
      12
      down vote

      favorite
      1









      up vote
      12
      down vote

      favorite
      1






      1





      Bear with me, I'm still new to QT and am having trouble wrapping my brain around how it does things.



      I've created and populated a QTreeView with two columns:



      class AppForm(QMainWindow):
      def __init__(self, parent = None):
      super(AppForm, self).__init__(parent)
      self.model = QStandardItemModel()
      self.view = QTreeView()
      self.view.setColumnWidth(0, 800)
      self.view.setEditTriggers(QAbstractItemView.NoEditTriggers)
      self.view.setModel(self.model)
      self.setCentralWidget(self.view)


      Everything's working great, except the columns are extremely narrow. I hoped that setColumnWidth(0, 800) would widen the first column, but it doesn't seem to be having any effect. What's the proper method for setting column widths?










      share|improve this question















      Bear with me, I'm still new to QT and am having trouble wrapping my brain around how it does things.



      I've created and populated a QTreeView with two columns:



      class AppForm(QMainWindow):
      def __init__(self, parent = None):
      super(AppForm, self).__init__(parent)
      self.model = QStandardItemModel()
      self.view = QTreeView()
      self.view.setColumnWidth(0, 800)
      self.view.setEditTriggers(QAbstractItemView.NoEditTriggers)
      self.view.setModel(self.model)
      self.setCentralWidget(self.view)


      Everything's working great, except the columns are extremely narrow. I hoped that setColumnWidth(0, 800) would widen the first column, but it doesn't seem to be having any effect. What's the proper method for setting column widths?







      python pyqt pyqt4 qtreeview






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Mar 16 '14 at 3:38









      ekhumoro

      75.5k15108180




      75.5k15108180










      asked Dec 2 '11 at 23:38









      ashground

      104229




      104229
























          2 Answers
          2






          active

          oldest

          votes

















          up vote
          15
          down vote



          accepted










          When you call setColumnWidth, Qt will do the equivalent of:



          self.view.header().resizeSection(column, width)


          Then, when you call setModel, Qt will (amongst other things) do the equivalent of:



          self.view.header().setModel(model)


          So the column width does get set - just not on the model the tree view ends up with.



          tl;dr: set the column width after you set the model.



          EDIT



          Here's a simple demo script based on your example:



          from PyQt4 import QtGui, QtCore

          class Window(QtGui.QMainWindow):
          def __init__(self):
          QtGui.QMainWindow.__init__(self)
          self.model = QtGui.QStandardItemModel()
          self.view = QtGui.QTreeView()
          self.view.setEditTriggers(QtGui.QAbstractItemView.NoEditTriggers)
          self.view.setModel(self.model)
          self.setCentralWidget(self.view)
          parent = self.model.invisibleRootItem()
          for item in 'One Two Three Four'.split():
          parent.appendRow([
          QtGui.QStandardItem(item),
          QtGui.QStandardItem(),
          QtGui.QStandardItem(),
          ])
          self.view.setColumnWidth(0, 800)

          if __name__ == '__main__':

          import sys
          app = QtGui.QApplication(sys.argv)
          window = Window()
          window.show()
          sys.exit(app.exec_())





          share|improve this answer























          • I moved setColumnWidth below setModel, but it still doesn't seem to be having an effect. Is the problem that I haven't populated it or set the amount of columns yet?
            – ashground
            Dec 5 '11 at 18:32










          • @ashground. I've added a demo script to my answer that works for me.
            – ekhumoro
            Dec 5 '11 at 18:40










          • Awesome -- I moved setColumnWidth into a different function so that it's called after it populates the tree. Everything's working as expected now. Thanks for your help!
            – ashground
            Dec 5 '11 at 19:57


















          up vote
          9
          down vote













          self.view.resizeColumnToContents(0)


          This makes sure that given column's width and height are set to match with content.






          share|improve this answer























          • QTreeView' object has no attribute 'resizeColumnsToContents'
            – Sophus
            May 2 '17 at 18:51










          • resizeColumnToContents(), not Columns
            – Liao Zhuodi
            May 7 '17 at 13:30











          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',
          autoActivateHeartbeat: false,
          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%2f8364061%2fhow-do-you-set-the-column-width-on-a-qtreeview%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          15
          down vote



          accepted










          When you call setColumnWidth, Qt will do the equivalent of:



          self.view.header().resizeSection(column, width)


          Then, when you call setModel, Qt will (amongst other things) do the equivalent of:



          self.view.header().setModel(model)


          So the column width does get set - just not on the model the tree view ends up with.



          tl;dr: set the column width after you set the model.



          EDIT



          Here's a simple demo script based on your example:



          from PyQt4 import QtGui, QtCore

          class Window(QtGui.QMainWindow):
          def __init__(self):
          QtGui.QMainWindow.__init__(self)
          self.model = QtGui.QStandardItemModel()
          self.view = QtGui.QTreeView()
          self.view.setEditTriggers(QtGui.QAbstractItemView.NoEditTriggers)
          self.view.setModel(self.model)
          self.setCentralWidget(self.view)
          parent = self.model.invisibleRootItem()
          for item in 'One Two Three Four'.split():
          parent.appendRow([
          QtGui.QStandardItem(item),
          QtGui.QStandardItem(),
          QtGui.QStandardItem(),
          ])
          self.view.setColumnWidth(0, 800)

          if __name__ == '__main__':

          import sys
          app = QtGui.QApplication(sys.argv)
          window = Window()
          window.show()
          sys.exit(app.exec_())





          share|improve this answer























          • I moved setColumnWidth below setModel, but it still doesn't seem to be having an effect. Is the problem that I haven't populated it or set the amount of columns yet?
            – ashground
            Dec 5 '11 at 18:32










          • @ashground. I've added a demo script to my answer that works for me.
            – ekhumoro
            Dec 5 '11 at 18:40










          • Awesome -- I moved setColumnWidth into a different function so that it's called after it populates the tree. Everything's working as expected now. Thanks for your help!
            – ashground
            Dec 5 '11 at 19:57















          up vote
          15
          down vote



          accepted










          When you call setColumnWidth, Qt will do the equivalent of:



          self.view.header().resizeSection(column, width)


          Then, when you call setModel, Qt will (amongst other things) do the equivalent of:



          self.view.header().setModel(model)


          So the column width does get set - just not on the model the tree view ends up with.



          tl;dr: set the column width after you set the model.



          EDIT



          Here's a simple demo script based on your example:



          from PyQt4 import QtGui, QtCore

          class Window(QtGui.QMainWindow):
          def __init__(self):
          QtGui.QMainWindow.__init__(self)
          self.model = QtGui.QStandardItemModel()
          self.view = QtGui.QTreeView()
          self.view.setEditTriggers(QtGui.QAbstractItemView.NoEditTriggers)
          self.view.setModel(self.model)
          self.setCentralWidget(self.view)
          parent = self.model.invisibleRootItem()
          for item in 'One Two Three Four'.split():
          parent.appendRow([
          QtGui.QStandardItem(item),
          QtGui.QStandardItem(),
          QtGui.QStandardItem(),
          ])
          self.view.setColumnWidth(0, 800)

          if __name__ == '__main__':

          import sys
          app = QtGui.QApplication(sys.argv)
          window = Window()
          window.show()
          sys.exit(app.exec_())





          share|improve this answer























          • I moved setColumnWidth below setModel, but it still doesn't seem to be having an effect. Is the problem that I haven't populated it or set the amount of columns yet?
            – ashground
            Dec 5 '11 at 18:32










          • @ashground. I've added a demo script to my answer that works for me.
            – ekhumoro
            Dec 5 '11 at 18:40










          • Awesome -- I moved setColumnWidth into a different function so that it's called after it populates the tree. Everything's working as expected now. Thanks for your help!
            – ashground
            Dec 5 '11 at 19:57













          up vote
          15
          down vote



          accepted







          up vote
          15
          down vote



          accepted






          When you call setColumnWidth, Qt will do the equivalent of:



          self.view.header().resizeSection(column, width)


          Then, when you call setModel, Qt will (amongst other things) do the equivalent of:



          self.view.header().setModel(model)


          So the column width does get set - just not on the model the tree view ends up with.



          tl;dr: set the column width after you set the model.



          EDIT



          Here's a simple demo script based on your example:



          from PyQt4 import QtGui, QtCore

          class Window(QtGui.QMainWindow):
          def __init__(self):
          QtGui.QMainWindow.__init__(self)
          self.model = QtGui.QStandardItemModel()
          self.view = QtGui.QTreeView()
          self.view.setEditTriggers(QtGui.QAbstractItemView.NoEditTriggers)
          self.view.setModel(self.model)
          self.setCentralWidget(self.view)
          parent = self.model.invisibleRootItem()
          for item in 'One Two Three Four'.split():
          parent.appendRow([
          QtGui.QStandardItem(item),
          QtGui.QStandardItem(),
          QtGui.QStandardItem(),
          ])
          self.view.setColumnWidth(0, 800)

          if __name__ == '__main__':

          import sys
          app = QtGui.QApplication(sys.argv)
          window = Window()
          window.show()
          sys.exit(app.exec_())





          share|improve this answer














          When you call setColumnWidth, Qt will do the equivalent of:



          self.view.header().resizeSection(column, width)


          Then, when you call setModel, Qt will (amongst other things) do the equivalent of:



          self.view.header().setModel(model)


          So the column width does get set - just not on the model the tree view ends up with.



          tl;dr: set the column width after you set the model.



          EDIT



          Here's a simple demo script based on your example:



          from PyQt4 import QtGui, QtCore

          class Window(QtGui.QMainWindow):
          def __init__(self):
          QtGui.QMainWindow.__init__(self)
          self.model = QtGui.QStandardItemModel()
          self.view = QtGui.QTreeView()
          self.view.setEditTriggers(QtGui.QAbstractItemView.NoEditTriggers)
          self.view.setModel(self.model)
          self.setCentralWidget(self.view)
          parent = self.model.invisibleRootItem()
          for item in 'One Two Three Four'.split():
          parent.appendRow([
          QtGui.QStandardItem(item),
          QtGui.QStandardItem(),
          QtGui.QStandardItem(),
          ])
          self.view.setColumnWidth(0, 800)

          if __name__ == '__main__':

          import sys
          app = QtGui.QApplication(sys.argv)
          window = Window()
          window.show()
          sys.exit(app.exec_())






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Dec 5 '11 at 18:39

























          answered Dec 3 '11 at 0:57









          ekhumoro

          75.5k15108180




          75.5k15108180












          • I moved setColumnWidth below setModel, but it still doesn't seem to be having an effect. Is the problem that I haven't populated it or set the amount of columns yet?
            – ashground
            Dec 5 '11 at 18:32










          • @ashground. I've added a demo script to my answer that works for me.
            – ekhumoro
            Dec 5 '11 at 18:40










          • Awesome -- I moved setColumnWidth into a different function so that it's called after it populates the tree. Everything's working as expected now. Thanks for your help!
            – ashground
            Dec 5 '11 at 19:57


















          • I moved setColumnWidth below setModel, but it still doesn't seem to be having an effect. Is the problem that I haven't populated it or set the amount of columns yet?
            – ashground
            Dec 5 '11 at 18:32










          • @ashground. I've added a demo script to my answer that works for me.
            – ekhumoro
            Dec 5 '11 at 18:40










          • Awesome -- I moved setColumnWidth into a different function so that it's called after it populates the tree. Everything's working as expected now. Thanks for your help!
            – ashground
            Dec 5 '11 at 19:57
















          I moved setColumnWidth below setModel, but it still doesn't seem to be having an effect. Is the problem that I haven't populated it or set the amount of columns yet?
          – ashground
          Dec 5 '11 at 18:32




          I moved setColumnWidth below setModel, but it still doesn't seem to be having an effect. Is the problem that I haven't populated it or set the amount of columns yet?
          – ashground
          Dec 5 '11 at 18:32












          @ashground. I've added a demo script to my answer that works for me.
          – ekhumoro
          Dec 5 '11 at 18:40




          @ashground. I've added a demo script to my answer that works for me.
          – ekhumoro
          Dec 5 '11 at 18:40












          Awesome -- I moved setColumnWidth into a different function so that it's called after it populates the tree. Everything's working as expected now. Thanks for your help!
          – ashground
          Dec 5 '11 at 19:57




          Awesome -- I moved setColumnWidth into a different function so that it's called after it populates the tree. Everything's working as expected now. Thanks for your help!
          – ashground
          Dec 5 '11 at 19:57












          up vote
          9
          down vote













          self.view.resizeColumnToContents(0)


          This makes sure that given column's width and height are set to match with content.






          share|improve this answer























          • QTreeView' object has no attribute 'resizeColumnsToContents'
            – Sophus
            May 2 '17 at 18:51










          • resizeColumnToContents(), not Columns
            – Liao Zhuodi
            May 7 '17 at 13:30















          up vote
          9
          down vote













          self.view.resizeColumnToContents(0)


          This makes sure that given column's width and height are set to match with content.






          share|improve this answer























          • QTreeView' object has no attribute 'resizeColumnsToContents'
            – Sophus
            May 2 '17 at 18:51










          • resizeColumnToContents(), not Columns
            – Liao Zhuodi
            May 7 '17 at 13:30













          up vote
          9
          down vote










          up vote
          9
          down vote









          self.view.resizeColumnToContents(0)


          This makes sure that given column's width and height are set to match with content.






          share|improve this answer














          self.view.resizeColumnToContents(0)


          This makes sure that given column's width and height are set to match with content.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 12 at 10:00









          Bear

          305317




          305317










          answered Jun 9 '12 at 8:34









          galactor88

          9112




          9112












          • QTreeView' object has no attribute 'resizeColumnsToContents'
            – Sophus
            May 2 '17 at 18:51










          • resizeColumnToContents(), not Columns
            – Liao Zhuodi
            May 7 '17 at 13:30


















          • QTreeView' object has no attribute 'resizeColumnsToContents'
            – Sophus
            May 2 '17 at 18:51










          • resizeColumnToContents(), not Columns
            – Liao Zhuodi
            May 7 '17 at 13:30
















          QTreeView' object has no attribute 'resizeColumnsToContents'
          – Sophus
          May 2 '17 at 18:51




          QTreeView' object has no attribute 'resizeColumnsToContents'
          – Sophus
          May 2 '17 at 18:51












          resizeColumnToContents(), not Columns
          – Liao Zhuodi
          May 7 '17 at 13:30




          resizeColumnToContents(), not Columns
          – Liao Zhuodi
          May 7 '17 at 13:30


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f8364061%2fhow-do-you-set-the-column-width-on-a-qtreeview%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

          Xamarin.iOS Cant Deploy on Iphone

          Glorious Revolution

          Dulmage-Mendelsohn matrix decomposition in Python