Results 1 to 12 of 12

Thread: Custom widgets in layout

  1. #1
    Join Date
    Jan 2009
    Location
    Czech Republic
    Posts
    26
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Custom widgets in layout

    Hi... I have class called EncrypterBody its parent is QWidget and It contains widgets like QPushButton, QTextEdit, etc defined in its constructor as well as its its own layout. Now I want have two objects of EncrypterBody class been shown in one window one under the other.

    Something like this
    Qt Code:
    1. --------------------------------------
    2. | |
    3. --------------------------------------
    4. | |---------|
    5. | |---------|
    6. | |---------|
    7. | | |
    8. -------------------------------------|
    9. --------------------------------------
    10. | |
    11. --------------------------------------
    12. | |---------|
    13. | |---------|
    14. | |---------|
    15. | | |
    16. -------------------------------------|
    To copy to clipboard, switch view to plain text mode 

    I also tried to make class "layout" (which parent was QVBoxLayout) in which constructor I created two instances of EncrypterBody and then I tried to add them to the layout via addWidget.

    I can easily manage to show two objects of EncryterBody in two separated windows, but to show them in one seems to be too hard for newbie of my caliber :P
    Attached Files Attached Files

  2. #2
    Join Date
    Nov 2008
    Posts
    142
    Thanks
    3
    Thanked 20 Times in 20 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Custom widgets in layout

    You cannot show a layout only. A layout is something you apply to widgets to tell them how to place their contents.

    What you need to do is

    • create a window, e.g. QDialog
    • set it's layout using QDialog::setLayout() to QVBoxLayout
    • add your widgets to this layout (see QWidget::setLayout() for an example of how to do so)


    You do not need to create a class that inherits QVBoxLayout.

    For more information on layouts you should read the according section from the Qt documentation.

  3. #3
    Join Date
    Dec 2008
    Location
    Czech
    Posts
    44
    Thanks
    2
    Thanked 8 Times in 8 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: Custom widgets in layout

    Hi,
    the main problem is in constructor of you class EncrypterBody
    You actually forgot to show your gridLayout. Pass 'this' in constructor of QGridLayout object (line 1 here)

    Qt Code:
    1. // UspořádánÃ* layoutu
    2. QGridLayout *layoutBody = new QGridLayout(this);
    3. layoutBody->addWidget(keyPublic, 0, 0, 1, 1);
    4. layoutBody->addWidget(openFileName, 1, 0, 1, 1);
    5. layoutBody->addWidget(text, 2, 0, 2, 1);
    6. layoutBody->addLayout(layoutButtons, 2, 1);
    7. layoutBody->setRowMinimumHeight(3, TEXT_H - (3 * BUTT_H + 8));
    To copy to clipboard, switch view to plain text mode 
    this solves your problem. So you don't have to do another class Layout (if you really dont want it for some reason). It could be enough to do it in main like this

    Qt Code:
    1. #include <QApplication>
    2. #include <QtGui>
    3.  
    4. #include "sifrator.h"
    5.  
    6. int main(int argc, char *argv[])
    7. {
    8. QApplication sifrator(argc, argv);
    9.  
    10. QWidget mainWindow;
    11. mainWindow.show();
    12.  
    13. EncrypterBody *partEncryption = new EncrypterBody(&mainWindow);
    14. EncrypterBody *partDecryption = new EncrypterBody(&mainWindow);
    15.  
    16. QObject::connect(partEncryption, SIGNAL(clickedTranslate(QString)), partDecryption, SLOT(setText(QString)));
    17.  
    18. QVBoxLayout *layout = new QVBoxLayout(&mainWindow);
    19.  
    20. layout->addWidget(partEncryption);
    21. layout->addWidget(partDecryption);
    22.  
    23. return sifrator.exec();
    24. }
    To copy to clipboard, switch view to plain text mode 
    It was working for me.

    Tak preji hodne zdaru

    ahoj
    Last edited by jpn; 18th January 2009 at 18:41. Reason: missing [code] tags

  4. The following user says thank you to radek.z for this useful post:

    Palmik (21st January 2009)

  5. #4
    Join Date
    Jan 2009
    Location
    Czech Republic
    Posts
    26
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Custom widgets in layout

    Ok, I tried what you said and It works... for other who might learn from mistakes of mine
    This is the new code of main.cpp layout.h and layout.cpp exists no more :P
    Qt Code:
    1. #include <QApplication>
    2. #include <QVBoxLayout>
    3. #include <QDialog>
    4. //#include <QWidget>
    5.  
    6. #include "sifrator.h"
    7.  
    8. int main(int argc, char *argv[])
    9. {
    10. QApplication sifrator(argc, argv);
    11.  
    12. QDialog *window = new QDialog;
    13. EncrypterBody *partEncryption = new EncrypterBody;
    14. EncrypterBody *partDecryption = new EncrypterBody;
    15.  
    16. QObject::connect(partEncryption, SIGNAL(clickedTranslate(QString)), partDecryption, SLOT(setText(QString)));
    17.  
    18. QVBoxLayout *layout = new QVBoxLayout;
    19. layout->addWidget(partEncryption);
    20. layout->addWidget(partDecryption);
    21.  
    22. window->setLayout(layout);
    23. window->show();
    24.  
    25. return sifrator.exec();
    26. }
    To copy to clipboard, switch view to plain text mode 

    Edit: Radek.z was faster, sorry I did not refreshed the page before posting
    Thank you guys
    Last edited by Palmik; 18th January 2009 at 19:21. Reason: typo

  6. #5
    Join Date
    Jan 2009
    Location
    Czech Republic
    Posts
    26
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Signals and Slots

    When you helped me to solve my previous problem, second problem occurred.
    Since I am writing this program for learning purposes I am trying to put here as much things as possible :P

    This problem is connected to signals and slots
    I have class EncryptedBody and class Rsa. In Rsa class I have 4 problematic signals

    Qt Code:
    1. void encryptbarChange(int value);
    2. void encryptbarMaximum(int value);
    3. void decryptbarChange(int value);
    4. void decryptbarMaximum(int value);
    To copy to clipboard, switch view to plain text mode 

    those are emitted in two functions
    Qt Code:
    1. // String čÃ*slic převede na tring znaků
    2. QString Rsa::toCharacters(QString textEncrypted)
    3. {
    4. QString textNormal;
    5. QTextStream out(&textNormal);
    6. int i = (digitsN - 1); // počet čÃ*slic reprezentujÃ*cÃ* jeden znak - 1
    7. int cNumber = 0;
    8. int cDigit;
    9.  
    10. int size = textEncrypted.size();
    11. emit decryptbarMaximum(size);
    12.  
    13. for (int o = 0; o < textEncrypted.size(); o++)
    14. {
    15. QChar cChar_ = textEncrypted.data()[o]; // Vezme znak z QString na pozici o a vrátÃ* ho jako QChar
    16. char cChar = cChar_.toLatin1(); // Převede QChar na char
    17. cDigit = atoi(&cChar); // Převede char na int
    18.  
    19. cNumber += cDigit * pow(10, i); // Např. 123 = (1 * 10 ^ 2) + (2 * 10 ^ 1) + (1 * 10 ^ 0) --> tento cyklus spojÃ* čÃ*slice v čÃ*slo
    20. i--;
    21.  
    22. if (i == -1) // Pokud je i, které sloužÃ* jako exponent rovno -1 čÃ*slo je hotovo a jedem od začátku
    23. {
    24. cNumber = decryption(cNumber);
    25. textNormal.append(cNumber);
    26. i = (digitsN - 1);
    27. cNumber = 0;
    28. }
    29. emit decryptbarChange(o);
    30. }
    31. return textNormal;
    32. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. // String znaků převede na string čÃ*slic (jeden znak je pak pořadÃ* x čÃ*slic, kde x je počet čÃ*slic v N), zachovává bÃ*lé znaky
    2. QString Rsa::toDigits(QString textNormal)
    3. {
    4. QString textEncrypted;
    5. QTextStream out(&textEncrypted);
    6. int size = textNormal.size();
    7.  
    8. emit encryptbarMaximum(size);
    9.  
    10. for (int i = 0; i < size; i++)
    11. {
    12. QChar cChar = textNormal.data()[i];
    13. ulong cNumber = encryption(cChar.unicode()); // Převede znak na čÃ*slo a zaÅ¡ifruje ho.... encryption(cChar.unicode())
    14.  
    15. for (uint o = 1; o < digitsN; o++)
    16. {
    17. if (cNumber < pow(10, o))
    18. {
    19. textEncrypted.append('0');
    20. }
    21. }
    22. out << cNumber;
    23.  
    24. emit encryptbarChange(i);
    25. }
    26. return textEncrypted;
    27. }
    To copy to clipboard, switch view to plain text mode 

    I am connecting this signals in EncryptedBody class to two slots
    Qt Code:
    1. void EncrypterBody::textDecryption()
    2. {
    3. ulong n = keyNumber(key_1->text());
    4. uint d = keyNumber(key_2->text());
    5.  
    6. /*if (x > y)
    7.   {
    8.   uint n = x;
    9.   uint e = y;
    10.   //Rsa encryption = Rsa(false, n, d);
    11.   }
    12.   else
    13.   {
    14.   uint n = y;
    15.   uint e = x;
    16.   //Rsa encryption = Rsa(false, n, d);
    17.   }*/
    18.  
    19. Rsa *decryption = new Rsa(0, false, n, d);
    20.  
    21. QString textEncrypted = text->toPlainText();
    22. QString textNormal = decryption->toCharacters(textEncrypted);
    23.  
    24. QObject::connect(decryption, SIGNAL(decryptbarMaximum(int)), this, SLOT(setProgressBarMaximum(int)));
    25. QObject::connect(decryption, SIGNAL(decryptbarChange(int)), this, SLOT(setProgressBarValue(int)));
    26. emit clickedTranslate(textNormal);
    27. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. void EncrypterBody::textEncryption()
    2. {
    3. ulong n = keyNumber(key_1->text());
    4. uint e = keyNumber(key_2->text());
    5.  
    6. /*if (x > y)
    7.   {
    8.   uint n = x;
    9.   uint e = y;
    10.   //Rsa encryption = Rsa(true, n, e);
    11.   }
    12.   else
    13.   {
    14.   uint n = y;
    15.   uint e = x;
    16.   //Rsa encryption = Rsa(true, n, e);
    17.   }*/
    18.  
    19. Rsa *encryption = new Rsa(0, true, n, e);
    20.  
    21. QString textNormal = text->toPlainText();
    22. QString textEncrypted = encryption->toDigits(textNormal);
    23.  
    24. QObject::connect(encryption, SIGNAL(encryptbarMaximum(int)), this, SLOT(setProgressBarMaximum(int)));
    25. QObject::connect(encryption, SIGNAL(encryptbarChange(int)), this, SLOT(setProgressBarValue(int)));
    26. emit clickedTranslate(textEncrypted);
    27. }
    To copy to clipboard, switch view to plain text mode 

    and finaly in main.cpp I have this

    Qt Code:
    1. EncrypterBody *partEncryption = new EncrypterBody(0, true);
    2. EncrypterBody *partDecryption = new EncrypterBody(0, false);
    3.  
    4. QObject::connect(partEncryption, SIGNAL(clickedTranslate(QString)), partDecryption, SLOT(setText(QString)));
    5. QObject::connect(partDecryption, SIGNAL(clickedTranslate(QString)), partEncryption, SLOT(setText(QString)));
    To copy to clipboard, switch view to plain text mode 

    I have no problems with calling setText(QString) via clickedTranslate(QString) signal, but I have problems with setting max value a current value of progress bar which is shown, but it does not update when I click Translate button, which should emit clickedTranslated() which calls textEncryption() / textDecryption() in EncryptedBody class and those (textEncryption() /textDecryption()) use functions from Rsa class for encryption decryption and this functions should be emitting decryptbarChange(int), encryptbarChange(int), decryptbarMaximum(int), encryptbarChange(int)
    Attached Files Attached Files
    Last edited by Palmik; 21st January 2009 at 18:17.

  7. #6
    Join Date
    Dec 2008
    Location
    Czech
    Posts
    44
    Thanks
    2
    Thanked 8 Times in 8 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: Custom widgets in layout

    Hi,

    what is an encryption, which you connecting in line 177 and 178 of your sifrator.cpp file. Is it a member?
    (also decryption in line 206,207 in sifrator.cpp )

    radek

  8. #7
    Join Date
    Jan 2009
    Location
    Czech Republic
    Posts
    26
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Custom widgets in layout

    It is object of class Rsa... I am creating it on line 176 (the same applies for decryption object of Rsa class on line 205).
    Qt Code:
    1. Rsa *encryption = new Rsa(0, true, n, e);
    To copy to clipboard, switch view to plain text mode 
    When I run it from console it does not say that those signals or slots do not exist. Weird
    Last edited by Palmik; 22nd January 2009 at 17:32.

  9. #8
    Join Date
    Nov 2008
    Posts
    142
    Thanks
    3
    Thanked 20 Times in 20 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Custom widgets in layout

    Do I understand you correctly that your problem is that the progress bar is not updating? If you are doing calculations in the GUI thread that take some time, you should call QCoreApplication::processEvents() once in a while to give Qt a chance to update the GUI.

  10. #9
    Join Date
    Jan 2009
    Location
    Czech Republic
    Posts
    26
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Custom widgets in layout

    Yeah, you understood it correctly, ty, will try that

  11. #10
    Join Date
    Dec 2008
    Location
    Czech
    Posts
    44
    Thanks
    2
    Thanked 8 Times in 8 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: Custom widgets in layout

    Hi,
    did you figure out smthing?
    I was looking at this.
    in your fnc QString Rsa::toDigits(QString textNormal) I have tried to use
    Qt Code:
    1. int rec = QObject::receivers(SIGNAL(encryptbarMaximum(int)));
    2. qDebug()<<rec;
    To copy to clipboard, switch view to plain text mode 
    just after emiting this signal to see if it is connected to slots, and it show 0.
    I think you should connect the Rsa object in different way. I tried it in constructor of your EncrypterBody class and it worked, but of course there is a problem with inicialiazing the parametrs of Rsa object, so this you shoudl figure out.

    Radek

  12. The following user says thank you to radek.z for this useful post:

    Palmik (25th January 2009)

  13. #11
    Join Date
    Jan 2009
    Location
    Czech Republic
    Posts
    26
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Custom widgets in layout

    Hi Radek.
    With informations you provided I was able to do it :) I have to thank you again :)
    Now I just have to figure out why mine power and modulo thing ((x pow y) mod z)) is returning wrong numbers. And also comment the code better as well as do some stylish editing of the code :)
    Then I will surely came up with some other feature I would like to implement which wont work of course and then I will have kindly ask you to help me again :D

    Btw if you have any advice of improving the code, let me know :)

    Code is provided in attachment
    Attached Files Attached Files
    Last edited by Palmik; 24th January 2009 at 20:44.

  14. #12
    Join Date
    Dec 2008
    Location
    Czech
    Posts
    44
    Thanks
    2
    Thanked 8 Times in 8 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: Custom widgets in layout

    Hi,
    so now the fun work starting, right? .
    I dont really feel like to be good in advice for improving code. I just saw that you use czech language for comments so it was easier for me then for smbdy else to help you.
    So I wish you good luck and of course you welcome to ask me if some problem will come up. I will try to help you in case I will be able to.

Similar Threads

  1. Replies: 2
    Last Post: 16th May 2008, 15:39
  2. resizing events of a custom widget in a layout
    By Rooster in forum Qt Programming
    Replies: 7
    Last Post: 16th February 2008, 11:52
  3. picking geometry in qt custom widgets
    By notsonerdysunny in forum Qt Programming
    Replies: 2
    Last Post: 18th July 2007, 00:01
  4. Promoted widgets and layout boxes
    By notsonerdysunny in forum Qt Tools
    Replies: 3
    Last Post: 2nd May 2007, 15:15
  5. Custom plugin for a layout
    By cocheci in forum Qt Tools
    Replies: 2
    Last Post: 12th June 2006, 19:36

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.