There is a project with two QPlainTextEdit widgets:
//mainwindow.h #ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> namespace Ui { class MainWindow; } class MainWindow : public QMainWindow { Q_OBJECT public: explicit MainWindow(QWidget *parent = 0); ~MainWindow(); private slots: void on_plainTextEdit_textChanged(); private: Ui::MainWindow *ui; }; #endif // MAINWINDOW_H //mainwindow.cpp #include "mainwindow.h" #include "ui_mainwindow.h" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); } MainWindow::~MainWindow() { delete ui; } void MainWindow::on_plainTextEdit_textChanged() { ui->plainTextEdit_2->setPlainText(ui->plainTextEdit->toPlainText()); } } As can be seen from here: when entering text into plainTextEdit , the contents are automatically transferred to plainTextEdit2 .
And now I'm trying to write a test for this simple program. But I don’t know how to get to the plainTextEdit widget, because it is in the wilds ui->plainTextEdit and the pointer ui also private .
Here is the test class that I prepared:
#include <QString> #include <QtTest> #include <QCoreApplication> class Test_MainWindow : public QObject { Q_OBJECT public: Test_MainWindow(); private Q_SLOTS: void plainText(); //мой тестовый метод void testCase1(); }; void Test_MainWindow::plainText() { //??? QTest::keyClicks(ui->plainTextEdit, "Text"); QCOMPARE(ui->plainTextEdit_2->toPlainText(), QString("Text")); } Test_MainWindow::Test_MainWindow() { } void Test_MainWindow::testCase1() { QVERIFY2(true, "Failure"); } QTEST_MAIN(Test_MainWindow) #include "tst_mainwindow.moc" Here is a template that Qt Creator helps me to do and a little of my sketch.
I also tried to completely inherit from the MainWindow class, but for some reason the compiler gave me a bunch of errors and immediately had to abandon this idea.
What are the suggestions on this?
void on_plainTextEdit_textChanged(). I basically test her, not the widget. But he (on_plainTextEdit_textChanged) does not accept data as input. That's why IplainTextEdittoplainTextEdit. - Adam