Accessing QT combo and textbox values - python

I'm using Python 3 and QT4 with pyqt. I've been unable to access data from qcomboboxes and qplaintextedits.
I've read the documentation here:
http://doc.qt.io/qt-4.8/qcombobox.html
http://doc.qt.io/qt-4.8/qplaintextedit.html
The only thing on these pages that appears to describe how to pull the selected value are 'plainText' for the plain text box and 'currentText'for the combo box. These aren't explicitly described as the solution, but they're the only thing I found that makes sense.
currentText returns the following error: "AttributeError: 'QPlainTextEdit' object has no attribute 'plainText' "
currentText does not return an error, but returns the starting value of the combo box, regardless of what's selected.
Example code I'm using:
x = window.ui.tb_x.plainText()
y = window.ui.cb_y.currentText()
Any ideas?

Its toPlainText() method in qplaintextedit.
And try itemData( combo.currentIndex ) for combo.

Related

How to get the objectName of a modified QSpinBox?

I want to keep track of the user modifying the company's forecast in my custom made app.
I created it using qt designer and PyQt4, and I'm using QSpinBox for the quantities (easy way to control range of values and masking the fields to be number only).
The problem I'm having is when I want to get the QSpinBox that triggered my function.
At this moment it's being triggered using valueChanged.connect but could be using anything else.
I can get the int in the spinbox but not the spinbox's name.
Thanks beforehand for the help!
SOLUTION
The QSpinBox element
self.Item = QtGui.QSpinBox(self.centralwidget)
self.Item.setObjectName(_fromUtf8("ItemName"))
Trigger
self.Item.valueChanged.connect(self.foo)
The function it calls
def foo(self,obj):
sender = MainWindow.sender()
print sender.objectName()
In this case "MainWindow" is my QtCore.QObject
AS the question tagged C++. I am answering in both C++ and pyQt4. I never worked in pyQt4. So please excuse me for syntax.
In the "valueChanged" slot use QObject::sender() to get the spin box which triggered it.
And then call "objectName()".
QObject* obj = sender();
QString objName = obj->objectName();
May be in python:
sender = QtCore.QObject.sender()
str = sender.objectName()

Tix ComboBox -labelside option not working

My company is using Python 2.6 (yuck, I know, but it's my constraint). I need to make a little GUI that involves a ComboBox. I chose Tix, because that's what I have--not allowed to grab anything else.
Anyway, I'd like to set the label that the ComboBox has to the top side. According to the documentation at http://tix.sourceforge.net/dist/current/man/html/TixCmd/tixComboBox.htm, if I use "labelside" in the ComboBox constructor as a parameter, it should move the label to the top, like I want.
Unfortunately, when I do this, it gives me a strange error:
_tkinter.TclError: cannot assigned to static variable "-labelside"
CONTEXT: this is within a Python class that inherits from Tix.Frame. The first code example works perfectly, the other does not.
My constructor (not including 'labelside') looks like this:
combobox = Tix.ComboBox(self,
label="Available files: ",
selectmode='immediate',
dropdown=0,
editable=0,
variable=selectedfile,
options='listbox.height 5')
It works perfectly, as expected. I get a nice ComboBox in my window. The label is the left side, however--not what I want.
So, I try this:
combobox = Tix.ComboBox(self,
label="Available files: ",
labelside='top',
selectmode='immediate',
dropdown=0,
editable=0,
variable=selectedfile,
options='listbox.height 5')
That's when it gives me the error. I've scrounged the internet for answers, but have found only users who have the same unanswered question: why is it happening? It would appear that I'm following the documentation correctly.
I also tried substituting Tix.TOP for top, and it gave me the same error.
Any help or ideas would be greatly appreciated!

Get and set the active row in QTreeview programmatically (PyQt)

Is there a way to get and change the active row in a QTreeView (not QTreeWidget)? By active, I mean the row with the focus highlight, not the selected row. In the paint event, I can use QStyle.State_HasFocus to get the active row, but this doesn't seem to work elsewhere.
You can get/set the active row with the currentIndex() and setCurrentIndex() functions that you can find in both QTreeView and QItemSelectionModel (the latter is returned by QTreeView.selectionModel()).
And despite its name, the QItemSelectionModel handles the view current item, and the view selection independently.
Current item is the one which is indicated by the focus rectangle. You can change it using selectionModel function of the tree view. If you don't want to change currently selected items, pass QtGui.QItemSelectionModel.NoUpdate as a second parameter to setCurrentIndex method. Below is an example:
index = model.index(3, 0);
view.selectionModel().setCurrentIndex(index, QtGui.QItemSelectionModel.NoUpdate)
this should move current item to the item with index 3
hope this helps, regards
For me it got nothing here new to ask such question, why because simple; you can use Qt-Designer and create QTreeView and then create line edit, then link them using the action editor, then transform the UI file to Py file, then you will see how things work behind the scene.
You will find this if you try:
QtCore.QObject.connect(self.treeView, QtCore.SIGNAL(_fromUtf8("clicked(QModelIndex)")), self.test)

Getting visible text from a QTextEdit in PyQt

This is related to another question I found here that seems to be inactive for a few months, so I think it's worth asking again.
I have created a simple QDialog that has a QTextEdit and a QPushButton. This pops up in my application when a user right-clicks and selects the option to "add comments". I want them to be able to write free-form text and I'll just save whatever they write as a long string with no concern for new lines, etc.
When the user clicks the button, it executes code like this:
self.connect(accept_button,QtCore.SIGNAL('clicked()'),lambda arg=str(view_textedit.toPlainText()): self.updateGroupComments(arg))
def updateGroupComments(self,new_comment_str):
print "Updating user comment to have new string: " + new_comment_str
self.group_entry.list_of_user_comments[self.currentFrameCounter] = new_comment_str
This is not detecting the TextEdit text that is visible (it only detects whatever the text edit text is set to when it is created). How do I make a simple command that returns the currently visible text from a QTextEdit. Again, the function
toPlainText()
is not working correctly... it doesn't find the currently visible text, only whatever text was on screen before changes or additions started being made by the user.
If this can't be done without subclassing and appealing to cursor positions, it makes the whole thing seem worthless... so please keep suggestions only to those implemented without subclassing or manipulating cursors. It should be really simple and straightforward to just return all currently visible text... what am I missing?
Objects that are being bound to default arguments are evaluated at the definition time. The function is working correctly, it returns whatever was in the text field when it was executed. Your code simply calls it at the wrong moment. If you want to use lambda, then do:
self.connect(
accept_button, QtCore.SIGNAL('clicked()'),
lambda: self.updateGroupComments(str(view_textedit.toPlainText()))
)
Or make view_textedit an instance attribute instead, and do simply
self.connect(
accept_button, QtCore.SIGNAL('clicked()'), self.updateGroupComments
)
And change updateGroupComments to call self.view_textedit.toPlainText instead of taking an argument.
BTW, this is not PyQt specific, this is how Python works in general.
To illustrate my last comment, that lambda can very well be replaced with:
def slot():
self.updateGroupComments(str(view_textedit.toPlainText()))
self.connect(accept_button, QtCore.SIGNAL('clicked()'), slot)

Controlling location of Pmw.MessageDialog (and other similar widgets)

Python newbie here, newer still to Pmw:
I have the following method defined for showing a Pmw MessageDialog box, and it works as expected and the result value is returned and posted in edit1, which is a Tkinter.Text widget; 'self' here is a Tkinter.Frame. (running Win7-32 and Python v2.7.2):
def _showMessageBar(self):
dialog = Pmw.MessageDialog(self, title = 'DBox',defaultbutton = 0,buttons('OK',Cancel'), message_text = 'DBox')
dialog.iconname(dialog['title'])
try:
result = dialog.activate()
finally:
dialog.deactivate()
self.edit1.insert(END, result+ "\n")
The problem is, the call to dialog.activate() doesn't allow me to control the location of the messageBox.
If I change that call to:
result = dialog.activate(geometry = first+50+20)
then the messageBox widget is placed at the specified coordinates, but this has two side effects:
1) The messageBox widget now has the buttons of main window (close, minimize,maximize) rather than a dialog box (just the close 'X' button)
2) The result value is never posted to edit1.
Question: How do I control the location of the messageBox while maintaining the dialog box buttons/border and getting the value posted to the Text (edit1) widget.
TIA
The answer is that the geometry options need to be in quotes, I wasn't seeing the proper results because an exception was being thrown by the improper geometry specifier. This code:
result = dialog.activate(geometry = "first+50+20")
works fine.

Categories