简体   繁体   English

创建用于将一些文本放入QTextEdit的槽的问题

[英]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. 我已经做了相当深入的研究,因为我一直在努力解决插槽问题,但随着谷歌搜索结果越来越变得越来越紫,我决定只问SO友好=)请注意我没有使用QtCreator也没有动态的东西。 I need to: 我需要:

  1. declare some QStrings which are constant 声明一些不变的QStrings
  2. get some QStrings out of QLineEdits 从QLineEdits中获取一些QStrings
  3. add 1 and 2 加1和2
  4. finally, put them into QTextEdit when a button is clicked. 最后,单击一个按钮时将它们放入QTextEdit。

For the step 1, I declare QStrings like this: 对于第1步,我声明QStrings如下:

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 (eg line_b_b is the name of a QLineEdit): 然后,对于步骤2,我从QLineEdits中生成QStrings(例如,line_b_b是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): 对于步骤3,我将步骤1中的QStrings与步骤2中的QStrings添加到名为“doverka”的变量中(请不要介意这个西里尔字母):

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. 最后,在步骤4中,我尝试在按下按钮时将整体放入QTextEdit。 And I guess the problem is here. 我猜问题就在这里。 I create a QTextEdit named "text": 我创建了一个名为“text”的QTextEdit:

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. 我是Qt和C ++的新手,这就是创建插槽的原因。 Here I've tried this with a lambda function but I am obviously doing something wrong. 在这里,我尝试使用lambda函数,但我显然做错了。 Maybe I should just put the lambda function somewhere else before SLOT ? 也许我应该在SLOT之前将lambda函数放在其他地方? 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, eg in making QStrings out of QLineEdits (step2)?.. I'm pretty helpless and appreciate any useful tips greatly! 或者问题可能在某个地方更早,例如在QLineEdits中生成QStrings(步骤2)?我非常无助,非常感谢任何有用的提示! Very many thanks. 非常感谢。

You are trying to mix old style Qt signal/slot connection with new style which obviously does not work. 您正试图将旧式Qt信号/插槽连接与新风格混合,这显然不起作用。 lambdas can be used only with new style of connections. lambdas只能用于新的连接方式。 If you are using Qt 5 the connection could be like: 如果您使用的是Qt 5,则连接可能如下:

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. 您应该注意,在调用lambda之前不应该销毁textdoverka对象,因为它们是通过引用捕获的。

In case of using Qt 4.* you should use the old syntax. 如果使用Qt 4. *,您应该使用旧语法。 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: 您的类应该从包含以下插槽的QObject继承:

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

Also note that text and doverka should be members of the class. 另请注意, textdoverka应该是该类的成员。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM