Search code examples
c++qtqtexteditqlineeditslot

Problems creating a slot for putting some text into QTextEdit


I've done quite a thorough research as I've been struggling with a slot issue, but as the Google search results steadily grew more and more purple, I decided just to ask the SO pals =) Please mind that I am not using QtCreator nor any dynamic stuff. I need to:

  1. declare some QStrings which are constant
  2. get some QStrings out of QLineEdits
  3. add 1 and 2
  4. finally, put them into QTextEdit when a button is clicked.

For the step 1, I declare QStrings like this:

QString set_1 = "ООО «Хеллманн» (129343, г. Москва, ул. Уржумская, д. 4, стр. 14, ИНН 7722637955, ОГРН 1087746168476) доверяет забор груза - ";
QString set_2 = " - перегружаемого из контейнера ";
QString set_3 = ", в количестве ";
QString set_4 = " паллет, весом ";
QString set_5 = " кг, водителю ";
QString set_6 = ", паспорт ";
QString set_7 = " выдан ";
QString set_8 = ".";
QString set_9 = " На автотранспортном средстве марки ";
QString set_10 = " - ";
QString set_11 = ", прицеп: ";

Then, for the step 2, I make QStrings out of QLineEdits like this (e.g. line_b_b is the name of a QLineEdit):

QString a = line_b_b.text();
QString b = line_b_a.text();
QString c = line_b_c.text();
QString d = line_b_d.text();
QString e = line_a_b.text();
QString f = line_a_a.text();
QString g = line_a_c.text();
QString h = line_a_d.text();
QString i = line_c_b.text();
QString j = line_c_a.text();
QString k = line_c_c.text();

For the step 3, I add the QStrings from the step 1 with those from the step 2 into a variable named "doverka" (please don't mind this cyrillic stuff):

QString doverka = set_1+a+set_2+b+set_3+c+set_4+d+set_5+e+set_6+f+set_7+g+h+set_8+set_9+i+set_10+j+set_11+k+set_8;

Finally, in the step 4, I try to put the whole into QTextEdit when a button is pushed. And I guess the problem is here. I create a QTextEdit named "text":

QTextEdit text (&dw);
text.show(); 

And then I try to create a slot and I presume I am doing this in a totally wrong way as it simply doesn't work:

QPushButton btn_t ("Создать текст", &dw);
   QObject::connect(
      &btn_t,
      SIGNAL(clicked()),
      &text,
      SLOT([dover](){return text.setText(doverka)}));
   btn_t.show();

I am new to Qt as well as to C++ and that's why poor at slot creation. Here I've tried this with a lambda function but I am obviously doing something wrong. Maybe I should just put the lambda function somewhere else before SLOT? My slot is not recognized as such when the prog is being compiled, I get the "no such slot" notification. Or maybe the problem is somewhere earlier, e.g. in making QStrings out of QLineEdits (step2)?.. I'm pretty helpless and appreciate any useful tips greatly! Very many thanks.


Solution

  • You are trying to mix old style Qt signal/slot connection with new style which obviously does not work. lambdas can be used only with new style of connections. If you are using Qt 5 the connection could be like:

    QObject::connect(
      &btn_t,
      &QPushButton::clicked,
      [&text, &doverka](){
         text.setText(doverka);
      });
    

    You should be careful that text and doverka objects should not be destroyed before the lambda is invoked, as they are captured by reference.

    In case of using Qt 4.* you should use the old syntax. In your case just provide a slot in your class and connect the signal it:

    QObject::connect(
      &btn_t,
      SIGNAL(clicked()),
      this,
      SLOT(onClicked()));
    

    Your class should inherit from QObject containing a slot like:

    public slots:
        void onClicked() {
             text.setText(doverka);
         }
    

    Also note that text and doverka should be members of the class.