Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. how to check if the Cellwidget of QTableWidget is QLineEdit

how to check if the Cellwidget of QTableWidget is QLineEdit

Scheduled Pinned Locked Moved Unsolved Qt for Python
8 Posts 6 Posters 1.7k Views 3 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Q Offline
    Q Offline
    Qt Enthusiast
    wrote on last edited by
    #1

    I have

    table = QtGui.QtableWidget(3,2)

    table.setCellWidget(0,0,QtGui.QLabel("Name"))
    table.setCellWidget(0,1,QtGui.QTextBrowserl())

    table.setCellWidget(1,0,QtGui.QLabel("item1))

    table.setCellWidget(1,1,QtGui.QLabel("item1_label))

    table.setCellWidget(2,0,QtGui.QLabel("item2"))
    table.setCellWidget(2,1,QtGui.QLabel("QLineEdit))

    how to check table.cellWidget(0,1) is a QtextBrowser and can be edited

    jsulmJ SGaistS 2 Replies Last reply
    0
    • Q Qt Enthusiast

      I have

      table = QtGui.QtableWidget(3,2)

      table.setCellWidget(0,0,QtGui.QLabel("Name"))
      table.setCellWidget(0,1,QtGui.QTextBrowserl())

      table.setCellWidget(1,0,QtGui.QLabel("item1))

      table.setCellWidget(1,1,QtGui.QLabel("item1_label))

      table.setCellWidget(2,0,QtGui.QLabel("item2"))
      table.setCellWidget(2,1,QtGui.QLabel("QLineEdit))

      how to check table.cellWidget(0,1) is a QtextBrowser and can be edited

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @Qt-Enthusiast Not a Qt question, but Python

      isinstance(obj, QLineEdit)
      

      See https://stackoverflow.com/questions/152580/whats-the-canonical-way-to-check-for-type-in-python

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      3
      • mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on last edited by mrjj
        #3

        @Denni-0
        Hi
        She means from code since the interface returns the cell widget as QWidget * if asked.
        So the question could also have been "what to use in python when we use qobject_cast<type *>
        in c++"

        1 Reply Last reply
        1
        • mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by mrjj
          #4

          @Denni-0
          Well a lookup would be great for her also for reading data later.

          Anyway, i realized the answer with isinstance is not complete.

          What would be the proper way to call a QtextBrowser function via widget pointer
          in python ?

          In c++ it goes like

          QTextBrowser * browser= qobject_cast<QTextBrowser *> ( pointer );
          if(browser)
          browser->SomeBrowserFunction()

          so in python ?

          if isinstance(obj, QTextBrowser ) :
          super(obj,self).SomeBrowserFunction()

          or what is the correct way ?

          can you simply try to call it ? ( duck typing )

          obj.SomeBrowserFunction();

          JonBJ 1 Reply Last reply
          0
          • mrjjM mrjj

            @Denni-0
            Well a lookup would be great for her also for reading data later.

            Anyway, i realized the answer with isinstance is not complete.

            What would be the proper way to call a QtextBrowser function via widget pointer
            in python ?

            In c++ it goes like

            QTextBrowser * browser= qobject_cast<QTextBrowser *> ( pointer );
            if(browser)
            browser->SomeBrowserFunction()

            so in python ?

            if isinstance(obj, QTextBrowser ) :
            super(obj,self).SomeBrowserFunction()

            or what is the correct way ?

            can you simply try to call it ? ( duck typing )

            obj.SomeBrowserFunction();

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by JonB
            #5

            @mrjj
            You can go

            if isinstance(obj, QTextBrowser):
                obj.SomeBrowserFunction()
            

            You could also test method presence on the object:

            if hasattr(obj, "SomeBrowserFunction"):
                obj.SomeBrowserFunction()
            

            Or, not particularly my taste, but:

            try:
                obj.SomeBrowserFunction()
            except AttributeError:
                pass
            

            There is no "object-type-casting" is Python. As @jsulm said, isinstance(object, type) is what you have, as a boolean test.

            Also, if you don't do the isinstance() test, the second two examples above will work if obj has some method named SomeBrowserFunction on it, but it may not be a QTextBrowser instance, just something which happens to have a function of that name, which might do anything. Which may or may not bother you... :)

            mrjjM 1 Reply Last reply
            2
            • JonBJ JonB

              @mrjj
              You can go

              if isinstance(obj, QTextBrowser):
                  obj.SomeBrowserFunction()
              

              You could also test method presence on the object:

              if hasattr(obj, "SomeBrowserFunction"):
                  obj.SomeBrowserFunction()
              

              Or, not particularly my taste, but:

              try:
                  obj.SomeBrowserFunction()
              except AttributeError:
                  pass
              

              There is no "object-type-casting" is Python. As @jsulm said, isinstance(object, type) is what you have, as a boolean test.

              Also, if you don't do the isinstance() test, the second two examples above will work if obj has some method named SomeBrowserFunction on it, but it may not be a QTextBrowser instance, just something which happens to have a function of that name, which might do anything. Which may or may not bother you... :)

              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @JonB
              Ah, so you dont need to cast base pointer to gain access to derived child methods.
              Thank you.
              and its cool you can check for functions too.

              The try-catch seems a bit overkill but could be handy with multiple calls.

              JonBJ 1 Reply Last reply
              0
              • mrjjM mrjj

                @JonB
                Ah, so you dont need to cast base pointer to gain access to derived child methods.
                Thank you.
                and its cool you can check for functions too.

                The try-catch seems a bit overkill but could be handy with multiple calls.

                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on last edited by JonB
                #7

                @mrjj
                I've edited more, to scare or amuse you, depending on your inclinations :)

                The try ... except is a debate we have had elsewhere before. It is much more common in Python than C++. Some people will prefer to use that over an if test.

                1 Reply Last reply
                1
                • Q Qt Enthusiast

                  I have

                  table = QtGui.QtableWidget(3,2)

                  table.setCellWidget(0,0,QtGui.QLabel("Name"))
                  table.setCellWidget(0,1,QtGui.QTextBrowserl())

                  table.setCellWidget(1,0,QtGui.QLabel("item1))

                  table.setCellWidget(1,1,QtGui.QLabel("item1_label))

                  table.setCellWidget(2,0,QtGui.QLabel("item2"))
                  table.setCellWidget(2,1,QtGui.QLabel("QLineEdit))

                  how to check table.cellWidget(0,1) is a QtextBrowser and can be edited

                  SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  Hi,
                  @Qt-Enthusiast said in how to check if the Cellwidget of QTableWidget is QLineEdit:

                  I have

                  table = QtGui.QtableWidget(3,2)

                  table.setCellWidget(0,0,QtGui.QLabel("Name"))
                  table.setCellWidget(0,1,QtGui.QTextBrowserl())

                  table.setCellWidget(1,0,QtGui.QLabel("item1))

                  table.setCellWidget(1,1,QtGui.QLabel("item1_label))

                  table.setCellWidget(2,0,QtGui.QLabel("item2"))
                  table.setCellWidget(2,1,QtGui.QLabel("QLineEdit))

                  how to check table.cellWidget(0,1) is a QtextBrowser and can be edited
                  Why are you using cell widgets to show text in a QTableWidget ?

                  From the looks of it, you should use a QFormLayout to show that stuff.

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  3

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved