0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

PyQt4のQStringについて

Last updated at Posted at 2017-06-24

PyQt4のQString,QStringListなどについて

「PythonのPyQtによるクロスプラットフォームGUIアプリ作成入門」さまのexampleで少しハマったので記録。

python3でpyqt4を使う際に「QStringなんてありません」と言われてしまう。

AttributeError: module 'PyQt4.QtCore' has no attribute 'QString'

これはpython3ではQStringは自動的にstrで定義されているため。
QStringListなどにしても同様。QStringListはstrのリストとなる。

修正後のコード:

TableWidget.py
from PyQt4.QtGui import * 
from PyQt4.QtCore import * 
import sys
 
def main():  
    app = QApplication(sys.argv)
    table = QTableWidget()
    tableItem = QTableWidgetItem()
    
    # initiate table
    table.setWindowTitle("QTableWidget Example")
    table.setRowCount(4)
    table.setColumnCount(2)

    horzHeaders=["Name" , "Age"]
    table.setHorizontalHeaderLabels( horzHeaders );
    
    # set data
    table.setItem(0,0, QTableWidgetItem("Tom"))
    table.setItem(0,1, QTableWidgetItem("15"))
    table.setItem(1,0, QTableWidgetItem("Ken"))
    table.setItem(1,1, QTableWidgetItem("40"))
    table.setItem(2,0, QTableWidgetItem("Susie"))
    table.setItem(2,1, QTableWidgetItem("22"))
    table.setItem(3,0, QTableWidgetItem("Kevin"))
    table.setItem(3,1, QTableWidgetItem("65"))
 
    # show table
    table.show()
    return app.exec_()
 
if __name__ == '__main__':
    main()

参考と出典

How to create QString in PyQt4?
PyQt4 and Python v3

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?