如何在Maya上实现信号和插槽?我一直在阅读有关Qt信号和插槽的文章,并试图将其用于Maya API(OpenMaya(,但直到现在都没有成功。我希望有人能给我指明正确的方向。我想在单击按钮时将TextEditA的文本信息设置为TextEditB。我希望复制我在Python(PySide2(中写的东西。
我想做的是在单击convert按钮时将melPTextEdit setText设置为pyPTextEdit。
这就是我正在尝试做的(代码已经重写了一点,所以可能会有一些不一致。(:
serpensCmd.h
#ifndef _serpensCmd
#define _serpensCmd
#define slots Q_SLOTS
#include <stdio.h>
#include <maya/MPxCommand.h>
#include <maya/MArgList.h>
#include <maya/MQtUtil.h>
#include <QtWidgets/QLabel>
#include <QtWidgets/QGridLayout>
#include <QtWidgets/QPushButton>
#include <QtWidgets/QSpinBox>
#include <QtCore/QObject>
#include <QtCore/QString>
#include <QtWidgets/QMainWindow>
#include <QtWidgets/QWidget>
#include <QtCore/Qt>
#include <QtWidgets/QTextEdit>
#include <QtCore/QPointer>
#include <QtWidgets/QWidget>
#include <QtWidgets/QGridLayout>
#include <QtWidgets/QSpinBox>
#include <QtCore/QObject>
#include <QtCore/QString>
class SerpensMainWindow : public QMainWindow {
Q_OBJECT
public:
SerpensMainWindow(
QWidget* parent = MQtUtil::mainWindow(),
Qt::WindowFlags flags = Qt::WindowFlags()
);
virtual ~SerpensMainWindow();
QGridLayout* gridLayout;
QWidget* cw;
QLabel* melLabel;
QLabel* pyLabel;
QTextEdit* melPTextEdit;
QTextEdit* pyPTextEdit;
QPushButton* convertButton;
QPushButton* debugButton;
public slots:
void debug();
void convert();
};
class serpens : public MPxCommand
{
public:
serpens();
virtual ~serpens();
static void cleanup();
static void createWindow();
static void* creator() { return new serpens(); }
MStatus doIt(const MArgList&);
static QPointer<SerpensMainWindow> window;
static const MString commandName;
private:
};
#endif
serpensCmd.cpp
#include "serpensCmd.h"
#include <maya/MGlobal.h>
SerpensMainWindow::SerpensMainWindow(QWidget* parent, Qt::WindowFlags flags)
: QMainWindow(parent, flags)
{
QGridLayout* gridLayout = new QGridLayout(this);
QWidget* cw = new QWidget(this);
cw->setLayout(gridLayout);
setCentralWidget(cw);
QLabel* melLabel = new QLabel("mel", centralWidget());
QTextEdit* melPTextEdit = new QTextEdit(centralWidget());
QLabel* pyLabel = new QLabel("python", centralWidget());
QTextEdit* pyPTextEdit = new QTextEdit(centralWidget());
QPushButton* convertButton = new QPushButton("convert", centralWidget());
QPushButton* debugButton = new QPushButton("debug", centralWidget());
gridLayout->addWidget(melLabel, 0, 0, 1, 1);
gridLayout->addWidget(melPTextEdit, 1, 0, 1, 1);
gridLayout->addWidget(pyLabel, 0, 1, 1, 1);
gridLayout->addWidget(pyPTextEdit, 1, 1, 1, 1);
gridLayout->addWidget(convertButton, 2, 0, 1, 2);
gridLayout->addWidget(debugButton, 3, 0, 1, 2);
debugButton->connect(
debugButton, SIGNAL(clicked()),
this, SLOT(debug())
);
convertButton->connect(
convertButton, SIGNAL(clicked()),
this, SLOT(convert())
);
setWindowTitle("Serpens");
}
SerpensMainWindow::~SerpensMainWindow() {}
QPointer<SerpensMainWindow> serpens::window;
const MString serpens::commandName("Serpens");
void serpens::cleanup()
{
if (!window.isNull()) delete window;
}
void serpens::createWindow() {
if (window.isNull()) {
window = new SerpensMainWindow();
window->show();
}
else {
window->showNormal();
window->raise();
}
}
void SerpensMainWindow::convert() {
pyPTextEdit->setText(melPTextEdit->toPlainText());
MGlobal::displayInfo(
MQtUtil::toMString(pyPTextEdit->toPlainText())
);
}
void SerpensMainWindow::debug() {
MGlobal::displayInfo(MString("debug = "));
}
MStatus serpens::doIt( const MArgList& )
{
MStatus stat = MS::kSuccess;
serpens::createWindow();
return redoIt();
}
serpens::serpens()
{}
serpens::~serpens()
{
}
插件Main.cpp
#include "serpensCmd.h"
#include <maya/MFnPlugin.h>
MStatus initializePlugin( MObject obj )
{
MStatus status;
MFnPlugin plugin( obj, "test", "2019", "Any");
status = plugin.registerCommand("serpens", serpens::creator);
if (!status) {
status.perror(
MString("SerpensCmd - could not initialize plugin: ")
+ status.errorString()
);
return status;
}
return status;
}
MStatus uninitializePlugin( MObject obj )
{
MStatus status;
MFnPlugin plugin( obj );
serpens::cleanup();
status = plugin.deregisterCommand( "serpens" );
if (!status) {
status.perror(
MString("SerpensCmd - could not register '")
+ serpens::commandName + "' command: "
+ status.errorString()
);
return status;
}
return status;
}
PySide2示例:
from PySide2.QtWidgets import *
class window(QMainWindow):
def __init__(self, parent=None):
super(window, self).__init__(parent)
gridLayout = QGridLayout(self);
cw = QWidget(self);
cw.setLayout(gridLayout);
self.setCentralWidget(cw);
self.PTextEditA = QTextEdit(self.centralWidget());
self.PTextEditB = QTextEdit(self.centralWidget());
self.button = QPushButton("ok", self.centralWidget());
gridLayout.addWidget(self.PTextEditA, 0, 0, 1, 1);
gridLayout.addWidget(self.PTextEditB, 1, 0, 1, 1);
gridLayout.addWidget(self.button, 2, 0, 1, 2);
self.button.clicked.connect(self.testA)
def testA(self):
self.PTextEditB.setText(self.PTextEditA.text())
def main():
app = QApplication.instance()
mainWin = window()
mainWin.show()
sys.exit()
app.exec_()
main()
我尝试了Qt5语法进行连接,但它不起作用。在此处输入图像描述
debugButton->connect(
debugButton, &QPushButton::clicked,
this, &SerpensMainWindow::convert
);
我修改了您的代码,用toPlainText()
替换了不存在的self.PTextEditA.text()
。
from PySide2.QtWidgets import *
import sys
class window(QMainWindow):
def __init__(self, parent=None):
super(window, self).__init__(parent)
gridLayout = QGridLayout(self);
cw = QWidget(self);
cw.setLayout(gridLayout);
self.setCentralWidget(cw);
self.PTextEditA = QTextEdit(self.centralWidget());
self.PTextEditB = QTextEdit(self.centralWidget());
self.button = QPushButton("ok", self.centralWidget());
gridLayout.addWidget(self.PTextEditA, 0, 0, 1, 1);
gridLayout.addWidget(self.PTextEditB, 1, 0, 1, 1);
gridLayout.addWidget(self.button, 2, 0, 1, 2);
self.button.clicked.connect(self.testA)
def testA(self):
self.PTextEditB.setText(self.PTextEditA.toPlainText())
mainWin = window()
mainWin.show()
而且效果很好。