如何在mainwindow.cpp中使用replacementlg.ui?

时间:2016-12-20 17:08:26

标签: qt qt5 qt-creator qt-designer qtgui

我写了一个包含# Function for logging in and get a valid token def getToken(): gotToken = False while not gotToken: # Loop the cycle of logging in until valid token is received try: varUsername = raw_input("Enter your username: ") varPassword = getpass.getpass("Enter your password: ") reqAuthLogin = 'https://MY_URL?username=' + varUsername + '&password=' + varPassword # Send the login request varToken = json.loads(urllib2.urlopen(reqAuthLogin).read())['Token'] # Attempt to parse the JSON response and read the Token, if possible gotToken = True except: # If credential is invalid and no token returned os.system('cls') print 'Invalid credentials. Please try again. \n' os.system('cls') return varToken # Return the retrieved token at the end of this function mainwindow的项目。我想在replacedlg.ui中使用replacedlg.ui

我想在mainwindow.cpp中写ui->button之类的内容,但我不能。

谁能帮助我完成这项工作?

The whole project is here.

1 个答案:

答案 0 :(得分:2)

请勿尝试在类之间共享ui变量。这是糟糕的设计。而是在您的类中添加方法,这样您就可以执行所需的操作。

如果您要将行编辑的文本从replaceDlg班级发送到MainWindow班级,则应使用信号和广告位。这是一个例子:

#include <QtWidgets>

class MainWindow : public QMainWindow
{
    Q_OBJECT
public:
    MainWindow(QWidget *parent = Q_NULLPTR) : QMainWindow(parent)
    {
        setCentralWidget(&text_edit);
    }
public slots:
    void addText(const QString &text)
    {
        text_edit.append(text);
    }
private:
    QTextEdit text_edit;
};

class Dialog : public QDialog
{
    Q_OBJECT
public:
    Dialog(QWidget *parent = Q_NULLPTR) : QDialog(parent)
    {
        setLayout(new QHBoxLayout);
        QPushButton *send_button = new QPushButton("Send");
        layout()->addWidget(&line_edit);
        layout()->addWidget(send_button);
        connect(send_button, &QPushButton::clicked, this, &Dialog::sendButtonClicked);
    }
signals:
    void sendText(const QString &text);
private slots:
    void sendButtonClicked()
    {
        emit sendText(line_edit.text());
        accept();
    }
private:
    QLineEdit line_edit;
};

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    Dialog d;
    QObject::connect(&d, &Dialog::sendText, &w, &MainWindow::addText);
    w.show();
    d.show();    
    return a.exec();
}

#include "main.moc"