LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-17-2019, 06:34 AM   #1
Asciente
Member
 
Registered: May 2017
Location: Surakarta, Central java, Indonesia
Distribution: Debian and MX-linux
Posts: 53

Rep: Reputation: Disabled
How to set alignment for qtreeview cells?


How to create a QTreeView which it's items alignment are different even in the same row or column?

For example :
Row 0 Col 0 = left
Row 0 Col 1 = right
Row 1 Col 0 = right
Row 1 col 1 = left


I'm using Python 3.6.7 and PyQt5
Thanks_
 
Old 01-17-2019, 07:25 AM   #2
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
Quote:
Originally Posted by Asciente View Post
How to create a QTreeView which it's items alignment are different even in the same row or column?

For example :
Row 0 Col 0 = left
Row 0 Col 1 = right
Row 1 Col 0 = right
Row 1 col 1 = left


I'm using Python 3.6.7 and PyQt5

Thanks_
Great!


Where's your code?
 
1 members found this post helpful.
Old 01-17-2019, 07:42 AM   #3
Asciente
Member
 
Registered: May 2017
Location: Surakarta, Central java, Indonesia
Distribution: Debian and MX-linux
Posts: 53

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by rtmistler View Post
Great!


Where's your code?
This is my code to create the treeview :

Code:
from PyQt5 import Qt, QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):

    MainWindow.setObjectName("MainWindow")
    MainWindow.resize(850, 626)
    MainWindow.setStyleSheet("background-color: rgb(46, 52, 54);")
    self.centralWidget = QtWidgets.QWidget(MainWindow)
    self.centralWidget.setObjectName("centralWidget")

    self.model = QtGui.QStandardItemModel()
    self.model.setHorizontalHeaderLabels(("1","2","3","4","5"))

    self.treeView = QtWidgets.QTreeView(self.centralWidget)
    self.treeView.setModel(self.model)
    self.treeView.setColumnWidth(0, 100)
    self.treeView.setColumnWidth(1, 300)
    self.treeView.setColumnWidth(2, 150)
    self.treeView.setGeometry(QtCore.QRect(10, 270, 829, 241))
    self.treeView.setStyleSheet("background-color: rgb(255, 255, 255);\n"
"altErnate-background-color: rgb(210, 210, 210);\n"
"color: rgb(0, 0, 0);\n"
"font: 57 13pt \"Ubuntu\";")
    self.treeView.setAlternatingRowColors(True)
    self.treeView.setObjectName("treeView")
    self.treeView.setEditTriggers(self.treeView.NoEditTriggers)
And this is the code to insert data :

Code:
    self.model.insertRow(0)
    self.model.setData(self.model.index(0, 0), "ONE")
    self.model.setData(self.model.index(0, 1), "TWO")
    self.model.setData(self.model.index(0, 2), "THREE")
    self.model.setData(self.model.index(0, 3), "FOUR")
    self.model.setData(self.model.index(0, 4), "FIVE")
 
Old 01-17-2019, 07:54 AM   #4
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
You set the column's anchor to the east for right alignment and to the west for left alignment.

https://docs.python.org/2/library/tt...reeview.column
https://www.tutorialspoint.com/python/tk_anchors.htm
 
Old 01-17-2019, 08:49 AM   #5
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,225

Rep: Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320
Uh, rtmistler?

He's using Qt. Your links are for TK.

Last edited by dugan; 01-17-2019 at 08:54 AM.
 
Old 01-17-2019, 09:55 AM   #6
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
Valid point.

Most alignments in Qt are Qt::AlignLeft, or Qt::AlignRight and those would be applied to the specific resource it is needed for.
 
Old 01-18-2019, 11:06 PM   #7
Asciente
Member
 
Registered: May 2017
Location: Surakarta, Central java, Indonesia
Distribution: Debian and MX-linux
Posts: 53

Original Poster
Rep: Reputation: Disabled
I found it

Code:
self.model.setData(self.model.index(0, 0), QtCore.Qt.AlignLeft, QtCore.Qt.TextAlignmentRole)
self.model.setData(self.model.index(0, 1), QtCore.Qt.AlignRight, QtCore.Qt.TextAlignmentRole)
self.model.setData(self.model.index(1, 0), QtCore.Qt.AlignRight, QtCore.Qt.TextAlignmentRole)
self.model.setData(self.model.index(1, 1), QtCore.Qt.AlignLeft, QtCore.Qt.TextAlignmentRole)
Thanks for your help guys
 
  


Reply

Tags
python3



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] QTreeView & inherited QAbstractItemModel doesn't display anything redfox2807 Programming 2 03-30-2013 11:33 PM
openoffice-2.0 calc --cells missing when opened in ms format .xls sailajabhandaru Linux - Software 1 11-18-2005 10:29 PM
oocalc drop down cells zippo85 Linux - Software 1 10-13-2005 12:53 AM
CSS - problem with margins in cells J_K9 Programming 2 06-30-2005 11:10 AM
OOO Calc Paste shifts cells LasseW Linux - Software 0 02-03-2005 11:25 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:38 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration