#include "mainwindow.h"
#include <QApplication>
#include<QLineEdit>
#include<QPushButton>
#include<QHBoxLayout>
#include<QtDebug>
#include<QWidget>
QPushButton * createLineButton(QLineEdit *edit){
QPushButton *button = new QPushButton();
QHBoxLayout *layout = new QHBoxLayout();
button->setCursor(Qt::ArrowCursor);
button->setStyleSheet("background-color:cyan;border_radiusL5px;");
layout->addStretch();
layout->addWidget(button);
layout->setContentsMargins(0,0,0,0);
edit->setLayout(layout);
return button;
}
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QWidget ui;
int m=100;
QLineEdit *lineedit = new QLineEdit(&ui);
QPushButton *btn = createLineButton(lineedit);
lineedit->setGeometry(50,50,150,40);
QObject::connect(btn,&QPushButton::clicked,&ui,[=]()mutable{
qDebug()<<"clicked";
lineedit->setText(QString::number(m));
m++;
});
ui.resize(600,400);
ui.show();
return a.exec();
}
运行效果:
python实现
import sys
from PySide6.QtWidgets import *
from PySide6.QtCore import *
def createLineButton(lineedit:QLineEdit)->QPushButton:
button = QPushButton()
layout = QHBoxLayout()
button.setCursor(Qt.CursorShape.ArrowCursor)
button.setStyleSheet("backgound-color:cyan;")
layout.addStretch(0)
layout.addWidget(button)
layout.setContentsMargins(0,0,0,0)
lineedit.setLayout(layout)
return button
a:int =100
def on_btn_click():
global a
edit.setText(str(a))
a+=1
if __name__ == '__main__':
app = QApplication(sys.argv)
win = QWidget()
edit:QLineEdit = QLineEdit(parent=win)
edit.setGeometry(40,40,120,30)
btn:QPushButton = createLineButton(edit)
btn.clicked.connect(on_btn_click)
win.show()
sys.exit(app.exec())
运行