1. Create a combo box2. Make line edit object from combo box3. Center align the line edit4. Make the line edit on editable,Note : Because of the line edit object combo box will become editable therefore there is a need to make it non editable,In order to center align the text we have to do the following –,In this article we will see how we can center align the text of combo box and combo box should remain non-editable. By default the combo box text is left align and combo box don’t have any alignment method of its own.
Syntax :
# getting the line edit of combo box line_edit = self.combo_box.lineEdit() # setting line edit alignment to the center line_edit.setAlignment(Qt.AlignCenter) # setting line edit to read only line_edit.setReadOnly(True)
You can use setAlignment
method:
from PyQt4
import QtGui, QtCore
class Window(QtGui.QWidget):
def __init__(self):
QtGui.QWidget.__init__(self)
layout = QtGui.QVBoxLayout(self)
self.combo = QtGui.QComboBox()
self.combo.setEditable(True)
self.combo.lineEdit().setAlignment(QtCore.Qt.AlignCenter)
self.combo.addItems('One Two Three Four Five'.split())
layout.addWidget(self.combo)
if __name__ == '__main__':
import sys
app = QtGui.QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec_())
but still I cant find a method how to center the items shown in the list? ,a similar question is already answered here: How to center text in QComboBox?, [FIXED] How to run Pytorch on Macbook pro (M1) GPU? Issue I tried to train a model using PyTorch on my Macbook pro. It uses the new generation... , [FIXED] TypeError: expected str, bytes or os.PathLike object, not TextIOWrapper couldnt be solved Issue I want to open a file and then convert it from docx to zip. I am experiencing a bloc...
from PyQt4
import QtGui, QtCore
class Window(QtGui.QWidget):
def __init__(self):
QtGui.QWidget.__init__(self)
layout = QtGui.QVBoxLayout(self)
self.combo = QtGui.QComboBox()
self.combo.setEditable(True)
self.combo.lineEdit().setAlignment(QtCore.Qt.AlignCenter)
self.combo.addItems('One Two Three Four Five'.split())
layout.addWidget(self.combo)
if __name__ == '__main__':
import sys
app = QtGui.QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec_())
One possible option is to use a delegate:
from PyQt4
import QtGui, QtCore
class AlignDelegate(QtGui.QStyledItemDelegate):
def initStyleOption(self, option, index):
super(AlignDelegate, self).initStyleOption(option, index)
option.displayAlignment = QtCore.Qt.AlignCenter
class Window(QtGui.QWidget):
def __init__(self):
QtGui.QWidget.__init__(self)
layout = QtGui.QVBoxLayout(self)
self.combo = QtGui.QComboBox()
delegate = AlignDelegate(self.combo)
self.combo.setItemDelegate(delegate)
self.combo.setEditable(True)
self.combo.lineEdit().setAlignment(QtCore.Qt.AlignCenter)
self.combo.addItems('One Two Three Four Five'.split())
layout.addWidget(self.combo)
if __name__ == '__main__':
import sys
app = QtGui.QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec_())
Qt Centre is a community site devoted to programming in C++ using the Qt framework. Over 90 percent of questions asked here gets answered. If you are looking for information about Qt related issue — register and post your question., QComboBox text alignment via CSS, and QComboBox ListItem Height, If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below. , QComboBox text alignment By mentalmushroom in forum Qt Programming Replies: 1 Last Post: 31st January 2012, 21:33
QComboBox {
border: 1 px solid darkgray;border - radius: 0 px;padding - right: 20 px;min - width: 6 em;color: black;
}
QComboBox: !editable, QComboBox::drop - down: editable {
selection - color: rgb(0, 150, 200);
background: white;
}
QComboBox::drop - down {
subcontrol - origin: padding;
subcontrol - position: top right;
width: 15 px;
height: 30 px;
border - width: 0 px;
border - left - width: 1 px;
border - left - color: darkgray;
border - left - style: solid; /* just a single line */
border - top - right - radius: 0 px; /* same radius as the QComboBox */
border - bottom - right - radius: 0 px;
}
QComboBox::down - arrow {
image: url(: /images/icons / caret - down_262626_16.png);
}
QComboBox::drop - down: on {
top: 0 px;left: 0 px;background: rgb(0, 150, 200);
}
QComboBox QAbstractItemView {
/* background-color: rgb(0, 150, 200); border-radius: 0px;*/
selection - background - color: white;
selection - color: rgb(0, 150, 200);
}
QComboBox {
border: 1 px solid darkgray;border - radius: 0 px;padding - right: 20 px;min - width: 6 em;color: black;
}
QComboBox: !editable, QComboBox::drop - down: editable {
selection - color: rgb(0, 150, 200);
background: white;
}
QComboBox::drop - down {
subcontrol - origin: padding;
subcontrol - position: top right;
width: 15 px;
height: 30 px;
border - width: 0 px;
border - left - width: 1 px;
border - left - color: darkgray;
border - left - style: solid; /* just a single line */
border - top - right - radius: 0 px; /* same radius as the QComboBox */
border - bottom - right - radius: 0 px;
}
QComboBox::down - arrow {
image: url(: /images/icons / caret - down_262626_16.png);
}
QComboBox::drop - down: on {
top: 0 px;left: 0 px;background: rgb(0, 150, 200);
}
QComboBox QAbstractItemView {
/* background-color: rgb(0, 150, 200); border-radius: 0px;*/
selection - background - color: white;
selection - color: rgb(0, 150, 200);
}
QComboBox {
border: 1 px solid darkgray;
border - radius: 0 px;
padding - right: 20 px;
min - width: 6 em;
color: black;
}
QComboBox: !editable, QComboBox::drop - down: editable {
selection - color: rgb(0, 150, 200);
background: white;
}
QComboBox::drop - down {
subcontrol - origin: padding;
subcontrol - position: top right;
width: 15 px;
height: 30 px;
border - width: 0 px;
border - left - width: 1 px;
border - left - color: darkgray;
border - left - style: solid; /* just a single line */
border - top - right - radius: 0 px; /* same radius as the QComboBox */
border - bottom - right - radius: 0 px;
}
QComboBox::down - arrow {
image: url(: /images/icons / caret - down_262626_16.png);
}
QComboBox::drop - down: on {
top: 0 px;
left: 0 px;
background: rgb(0, 150, 200);
}
QComboBox QAbstractItemView {
/* background-color: rgb(0, 150, 200);
border-radius: 0px;*/
selection - background - color: white;
selection - color: rgb(0, 150, 200);
}
#include "customcombobox.h"#include <QLineEdit> CustomComboBox::CustomComboBox(QWidget *parent) : QComboBox(parent){ this->setEditable(true); this->lineEdit()->setDisabled(true); this->lineEdit()->setReadOnly(true); this->lineEdit()->setAlignment(Qt::AlignRight);}
#include "customcombobox.h"#include <QLineEdit> CustomComboBox::CustomComboBox(QWidget *parent) : QComboBox(parent){ this->setEditable(true); this->lineEdit()->setDisabled(true); this->lineEdit()->setReadOnly(true); this->lineEdit()->setAlignment(Qt::AlignRight);}
#include "customcombobox.h"
#include <QLineEdit>
CustomComboBox::CustomComboBox(QWidget *parent) : QComboBox(parent)
{
this->setEditable(true);
this->lineEdit()->setDisabled(true);
this->lineEdit()->setReadOnly(true);
this->lineEdit()->setAlignment(Qt::AlignRight);
}
How to center text in PyQt ComboBox? ,Your idea is a good one except for one detail, translated and adapted from the official documents :, Automatically download files without extension with selenium
Note: The item c1 of the function more is the combo box.
import sys
from PyQt5.QtWidgets
import QMainWindow, QApplication, QComboBox, QTableWidgetItem, QMessageBox
from PyQt5.uic
import loadUi
from PyQt5.QtCore
import Qt
class SubwindowDatosEntrada(QMainWindow):
def __init__(self, root = None):
def mas(self):
nuevo = self.table_datosEntrada.rowCount()
self.table_datosEntrada.insertRow(nuevo)
for row in range(self.table_datosEntrada.rowCount() - 1,
self.table_datosEntrada.rowCount(), 1):
for rowe in range(self.table_datosEntrada.columnCount()):
#Combo Box
c1 = QComboBox()
c1.lineEdit().setAlignment(Qt.AlignCenter)
c1.addItems(['Casa', 'Vivienda'])
# Números en celdas centradas
item = QTableWidgetItem(str(self.table_datosEntrada.rowCount()))
itemb = QTableWidgetItem()
item.setTextAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
itemb.setTextAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
self.table_datosEntrada.setCellWidget(row, 1, c1)
self.table_datosEntrada.setCellWidget(row, 12, c2)
self.table_datosEntrada.setItem(row, 0, item)
self.table_datosEntrada.setItem(row, rowe, itemb)
if __name__ == "__main__":
app = QApplication(sys.argv)
widget = SubwindowDatosEntrada()
widget.show()
sys.exit(app.exec_())
You could do therefore:
c1 = QComboBox()
c1.addItems(['Casa', 'Vivienda'])
c1.setEditable(True)
c1.lineEdit().setReadOnly(True)
c1.lineEdit().setAlignment(Qt.AlignCenter)
for i in range(c1.count()):
c1.setItemData(i, Qt.AlignCenter, Qt.TextAlignmentRole)
In ComboBoxAdv, you can specify the text alignment by using its TextAlign property. The following code example illustrates the same.,Figure 1: Text alignment specified in ComboBoxAdv,The following screenshot illustrates the output., You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.
this.comboBoxAdv1.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
Me.comboBoxAdv1.TextAlign = System.Windows.Forms.HorizontalAlignment.Right
09/13/2021
The following example aligns the text in the Address text box on the Suppliers form to the right.
Forms("Suppliers").Controls("Address").TextAlign = 3
In the paintEvent(), we created QPainter object. Then, we the pen color, font type, font size, and the text itself with alignment.,By reimplementing QWidget::paintEvent(), we can create customized widgets and have complete control over their appearance.,We can draws a line as well as points. Here we reimplemented the paintEvent() again:,The common use of QPainter is inside a widget's paint event: Construct and customize (e.g. set the pen or the brush) the painter. QPainter provides highly optimized functions to do most of the drawing GUI programs require. It can draw everything from simple lines to complex shapes like pies and chords. It can also draw aligned text and pixmaps.
Here is the modified painterdialog.h:
// painterdialog.h
#ifndef PAINTERDIALOG_H
#define PAINTERDIALOG_H
#include <QDialog>
#include <QPainter>
namespace Ui {
class PainterDialog;
}
class PainterDialog : public QDialog
{
Q_OBJECT
public:
explicit PainterDialog(QWidget *parent = 0);
~PainterDialog();
private:
Ui::PainterDialog *ui;
protected:
void paintEvent(QPaintEvent *e);
};
#endif // PAINTERDIALOG_H
Here is the implementation file, painterdialog.cpp:
#include "painterdialog.h"
#include "ui_painterdialog.h"
PainterDialog::PainterDialog(QWidget * parent):
QDialog(parent),
ui(new Ui::PainterDialog) {
ui - > setupUi(this);
}
PainterDialog::~PainterDialog() {
delete ui;
}
void PainterDialog::paintEvent(QPaintEvent * e) {
QPainter painter(this);
painter.setPen(Qt::blue);
painter.setFont(QFont("Arial", 80));
painter.drawText(rect(), Qt::AlignCenter, "Qt");
}
We can draws a line as well as points. Here we reimplemented the paintEvent() again:
void PainterDialog::paintEvent(QPaintEvent * e) {
QPainter MyPainter(this);
QPen PointPen(Qt::red);
PointPen.setWidth(5);
QPen LinePen(Qt::green);
LinePen.setWidth(2);
QPoint p1;
p1.setX(100);
p1.setY(100);
QPoint p2;
p2.setX(300);
p2.setY(200);
MyPainter.setPen(PointPen);
MyPainter.drawPoint(p1);
MyPainter.drawPoint(p2);
MyPainter.setPen(LinePen);
MyPainter.drawLine(p1, p2);
QPen LinePen2(Qt::black);
LinePen2.setStyle(Qt::DashDotLine);
LinePen2.setWidth(3);
MyPainter.setPen(LinePen2);
MyPainter.drawLine(QPoint(300, 100), QPoint(100, 200));
}
In order to align the text that is shown in the text box area when RadMultiColumnComboBox is in RadDropDownStyle.DropDownList you should set the TextAlignment property of the TextboxContentElement as shown below:,I have an issue where I had originally set the combobox textbox alignment to the right with:,The raddropdownstyle.dropdownlist worked, but the alignment isn't working any longer. I tried to put this before and I tried after before it's the same. Am I doing something wrong? Here's what I have now:,And it worked correctly. But then I decided I didn't want the user to be able to input anything, only select from drop down. So I added raddropdownstyle.dropdownlist:
this.radMultiColumnComboBox1.DropDownStyle = RadDropDownStyle.DropDownList;
this.radMultiColumnComboBox1.MultiColumnComboBoxElement.TextboxContentElement.TextAlignment = ContentAlignment.MiddleRight;