[PyQt] QPlainTextEdit not created in a widget through a QAction

Mark Summerfield list at qtrac.plus.com
Mon Jun 7 16:32:20 BST 2010


On 2010-06-07, Gerardo Gutierrez wrote:
> Thanks Mark, I did some things that you recommended:

Hi Gerardo,
 
> I changed the notebook class, so it inherits from QWIdget and no container
> need to be used, the notebook itself is going to be embedded in the
> QScrollArea:
> 
> class Notebook(QtGui.QWidget):
>     def __init__(self,parent):
>         QtGui.QWidget.__init__(self,parent)
> ...
> ...
>         self.setContextMenuPolicy(QtCore.Qt.ActionsContextMenu)
>         self.new_cell_action=QtGui.QAction("new cell",self)
>         self.addAction(self.new_cell_action)

That looks much better.

> 
self.connect(self.new_cell_action,QtCore.SIGNAL("triggered(bool)"),self.new
> _cell_slot)
> 
> The new_cell_slot calls the function to create the a new cell, which has
>  not changed since last mail (I'm not calculation yet the size of the
>  cells). The problem persists even if I make the QAction parent to be the
>  notebook. The cell are appended in the _cell_list attribute but not 
showed
>  when the QAction is calling the new_input_cell function.

I'd have to look at the whole code to figure that out and I don't have
time... hopefully someone else will take a look!

> This is a fragment of the main file:
> 
> class IpythonQt(QtGui.QMainWindow,Ui_Qtipython):
>     def __init__(self):
>         QtGui.QMainWindow.__init__(self)
>         self.setupUi(self)
> 
>         self.scrollArea = QtGui.QScrollArea(self.centralwidget)
>         self.scrollArea.setWidgetResizable(True)
>         self.verticalLayout_2.addWidget(self.scrollArea)
>         self.nb=Notebook(self.scrollArea)
>         self.scrollArea.setWidget(self.nb)
>         self.nb.new_input_cell()

This doesn't feel right to me.

Personally, I really don't like using Qt Designer for QMainWindows, only
for QDialogs and QWidgets. Also, QScrollArea should *not* be inherited.
It is a rare exception in Qt of a class designed to be used as is and
never inherited. (If you want inheritance use QAbstractScrollArea---but
don't bother it is pain for little gain.)

Anyway, back to the point. I am assuming that self.centralwidget is the
main window's central widget. If that is the case, then putting it into
a scroll area is wrong.

If your Notebook is the thing that belongs in the middle (as I suspect
it should since anything else can go inside dock widgets), then I'd
expect to see code like this:

    self.nb = Notebook()
    self.scrollArea = QScrollArea()
    self.scrollArea.setWidget(self.nb)
    self.setCentralWidget(self.scrollArea)

OTOH if you have a whole bunch of widgets in the middle and the Notebook
is just one of them, then I'd expect to see:

    self.nb = Notebook()
    self.scrollArea = QScrollArea()
    self.scrollArea.setWidget(self.nb)
    self.verticalLayout_2.addWidget(self.scrollArea)

(BTW Only give parents to things that are not put in a layout.)

I also don't like the call to self.nb.new_input_cell() in __init__().
You should use __init__() to create all the things the GUI uses, but
_not_ to make use of them. If you want to perform some action when
everything is set up then put it in a slot and at the end of __init__()
use something like this:

    QTimer.singleShot(0, self, SLOT(initialize))

and in your initialize() method do self.nb.new_input_cell() etc.

Sorry I couldn't see the answer to your problem, but hope the other
comments prove helpful!

[snip]

-- 
Mark Summerfield, Qtrac Ltd, www.qtrac.eu
    C++, Python, Qt, PyQt - training and consultancy
        "Rapid GUI Programming with Python and Qt" - ISBN 0132354187


More information about the PyQt mailing list