-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmybutton.cpp
More file actions
68 lines (67 loc) · 2.68 KB
/
Copy pathmybutton.cpp
File metadata and controls
68 lines (67 loc) · 2.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include "mybutton.h"
#include<QDebug>
#include<QObject>
#include<QKeyEvent>
#include<QEvent>
#include<QCoreApplication>
#include<QApplication>
#include<QString>
#include<QLineEdit>
#include<QTableWidgetItem>
MyButton::MyButton(QWidget *parent)
: QPushButton{parent}
{
connect(this,SIGNAL(pressed()),this,SLOT(func()));
}
void MyButton::func(){
//获取发送者
QPushButton *button=qobject_cast<QPushButton*>(QObject::sender());
//获取文本
QString keyinfo=button->text();
qDebug()<<"key:"<<keyinfo;
if(keyinfo.compare("Del")==0){
qDebug()<<"windows"<<this->window()->objectName();
//判断是光标聚焦的是什么控件
if(qApp->focusWidget()&&qApp->focusWidget()->inherits("QLineEdit")){
QLineEdit *text=qobject_cast<QLineEdit*>(QApplication::focusWidget());
QString old_text=text->text();
int length=old_text.length();
length--;
if(length<0){
text->clear();
}else{
QString new_text=old_text.mid(0,length);//删除最后一个
text->clear();
text->setText(new_text);
}
}
QTableWidget *tableWidget = qobject_cast<QTableWidget*>(qApp->focusWidget());
if (tableWidget) {
// 获取具有焦点的项目
QTableWidgetItem *focusedItem = tableWidget->currentItem();
if (focusedItem) {
// 检查具有焦点的项目是否是 QTableWidgetItem
if (dynamic_cast<QTableWidgetItem*>(focusedItem)) {
// 具有焦点的项目是 QTableWidgetItem
QTableWidgetItem *tableItem = dynamic_cast<QTableWidgetItem*>(focusedItem);
// 这里可以执行相关操作
QString old_text=tableItem->text();
int length=old_text.length();
length--;
if(length<0){
tableItem->setText("");
}else{
QString new_text=old_text.mid(0,length);//删除最后一个
tableItem->setText("");
tableItem->setText(new_text);
}
}
}
}
}else{
//定义按键事件
QKeyEvent *keyevent=new QKeyEvent(QEvent::KeyPress,keyinfo.toInt(),Qt::NoModifier,keyinfo);
//将按键事件发送到光标聚焦的控件
QCoreApplication::postEvent(QApplication::focusWidget(),keyevent);
}
}