我使用Qt在C ++中创建了一个应用程序。我希望如果用户点击了名为btnA
的按钮,那么程序应该仿效它,好像按下 A 一样。
我在谷歌搜索并找到了一个平台相关的解决方案。我希望它是跨平台的。有没有API?我更喜欢跨平台(或Linux)解决方案。
编辑: -
这是我的代码而不是wrking: -
#include "calculator.h"
#include "ui_calculator.h"
#include <QKeyEvent>
Calculator::Calculator(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::Calculator),
DisplayString("")
{
ui->setupUi(this);
setFixedSize(this->size());
//Connected all buttons to one slot
connect(ui->Button1,SIGNAL(clicked()),this,SLOT(CalButtonPressed()));
connect(ui->Button2,SIGNAL(clicked()),this,SLOT(CalButtonPressed()));
connect(ui->Button3,SIGNAL(clicked()),this,SLOT(CalButtonPressed()));
connect(ui->Button4,SIGNAL(clicked()),this,SLOT(CalButtonPressed()));
connect(ui->Button5,SIGNAL(clicked()),this,SLOT(CalButtonPressed()));
connect(ui->Button6,SIGNAL(clicked()),this,SLOT(CalButtonPressed()));
connect(ui->Button7,SIGNAL(clicked()),this,SLOT(CalButtonPressed()));
connect(ui->Button8,SIGNAL(clicked()),this,SLOT(CalButtonPressed()));
connect(ui->Button9,SIGNAL(clicked()),this,SLOT(CalButtonPressed()));
connect(ui->Button0,SIGNAL(clicked()),this,SLOT(CalButtonPressed()));
connect(ui->ButtonAddition,SIGNAL(clicked()),this,SLOT(CalButtonPressed()));
connect(ui->ButtonMinus,SIGNAL(clicked()),this,SLOT(CalButtonPressed()));
connect(ui->ButtonModulas,SIGNAL(clicked()),this,SLOT(CalButtonPressed()));
connect(ui->ButtonDivide,SIGNAL(clicked()),this,SLOT(CalButtonPressed()));
connect(ui->ButtonMultiplication,SIGNAL(clicked()),this,SLOT(CalButtonPressed()));
}
Calculator::~Calculator()
{
delete ui;
}
void Calculator::CalButtonPressed()
{
QPushButton *senderbtn = static_cast<QPushButton*>(sender());
QString string = senderbtn->text();
char ch = string.at(0).toLatin1();
// for testing I have taken only one button
QKeyEvent * event = new QKeyEvent(QEvent::KeyPress,Qt::Key_A,Qt::NoModifier,nullptr);
QApplication::postEvent(ui->Input,event);
}
的main.cpp
#include <QApplication>
#include <QFile>
#include <cstring>
#include "calculator.h"
int main(int argc,char **argv)
{
QApplication *app= new QApplication(argc,argv);
app->setWindowIcon(QIcon(":/Images/Icon.png"));
Calculator *cal = new Calculator(nullptr);
cal->show();
return app->exec();
}
它应该显示&#39; a&#39;在名为QLineEdit
的{{1}}小部件中
答案 0 :(得分:3)
您需要为此创建#!/usr/bin/env python
from PyQt5.QtCore import (QRectF)
from PyQt5.QtGui import (QPainter, QPixmap)
from PyQt5.QtWidgets import (QMainWindow, QApplication, QGraphicsObject, QGraphicsView, QGraphicsScene)
class TicTacToe(QGraphicsObject):
def __init__(self, helper):
super(TicTacToe, self).__init__()
self.mypixmap = QPixmap("exit1.png")
self.setAcceptHoverEvents(True)
def paint(self, painter, option, widget):
painter.setOpacity(1)
painter.drawPixmap(0,0, 512, 512, self.mypixmap)
painter.drawLine(2,2,20,20)
def boundingRect(self):
return QRectF(0,0,512, 512)
def hoverMoveEvent(self, event):
#print("hoverMoveEvent ", event.pos())
print("hoverMoveEvent", self.mapToScene(event.pos()))
def mousePressEvent(self, event):
#print("mousePressEvent", event.pos())
print("mousePressEvent", self.mapToScene(event.pos()))
class MyGraphicsView(QGraphicsView):
def __init__(self):
super(MyGraphicsView, self).__init__()
self.scene = QGraphicsScene(self)
self.setMouseTracking(True)
self.tic_tac_toe = TicTacToe(self)
self.myScale = 2
self.tic_tac_toe.setScale(self.myScale)
self.setScene(self.scene)
self.scene.addItem(self.tic_tac_toe)
def mouseMoveEvent(self, event):
print("mouseMoveEvent", self.mapToScene(event.pos()))
class Example(QMainWindow):
def __init__(self):
super(Example, self).__init__()
self.y = MyGraphicsView()
self.setCentralWidget(self.y)
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
w = Example()
w.show()
sys.exit(app.exec_())
,如下所示。
QKeyEvent
上面的语法创建QKeyEvent *event = new QKeyEvent(QEvent::KeyPress, Qt::Key_A, Qt::NoModifier, 0);
类型QKeyEvent
,按键为 A ,并告知在此过程中没有按下修饰符。
创建后,您可以使用KeyPress
将其添加到eventQueue。
postEvent
Qt您可以通过的文档,以便更好地了解您将要使用的内容:
我已经在Windows和Mac的跨平台解决方案中使用了上述内容。希望它也适用于linux。
更新:
由于您要使用文本更新行编辑,因此需要执行以下操作:
QApplication::postEvent(this, event);
这肯定会更新QKeyEvent *event = new QKeyEvent(QEvent::KeyPress, Qt::Key_A, Qt::NoModifier, QKeySequence(Qt::Key_A).toString());
// This is because QLineEdit does not deduce the text from the event type, but instead just adds the text it receives.
字符&#39; A&#39;按下QLineEdit
时。显然,在您使用上面创建的新button A
致电postEvent
后。