Results 1 to 16 of 16

Thread: problem with Qt

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: problem with Qt

    This is more object-oriented programming issue than anything Qt related. You should be able to come to your own conclusions here. It's all just a matter of exposing proper methods from objects/classes.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  2. #2
    Join Date
    May 2010
    Posts
    30
    Qt products
    Qt4

    Default Re: problem with Qt

    Yes I know this is easy but I just don't know how to do it.
    I've just started using qt and I know very little about C++.
    Can you help me please ?

    Thanks a lot.

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: problem with Qt

    Quote Originally Posted by deeee View Post
    I've just started using qt and I know very little about C++.
    So please do yourself a favour and get a decent knowledge of C++ first.

    To be honest I don't understand your problem... You are given an action which toggles whether the dock widget is shown or not and updates its state automatically based on the state of the dock widget. What more do you want?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  4. #4
    Join Date
    May 2010
    Posts
    30
    Qt products
    Qt4

    Default Re: problem with Qt

    Let me try to explain myself better then.
    My problem is that my dock widget is defined in a function but is used in another one. How can I access it ?
    I can't pass it as an argument because the signal is of fixed type and thus so is the function I want to create.

    More specifically (see the code above) :
    my dock widget is defined in the class FenPrincipale.
    I want to create a button that displays this dock when it is checked.
    This button sends a triggered signal when it is clicked on, and I want to connect it to a function called display_dock that is basically like this :
    void FenPrincipale::display_dock()
    {
    dock.ToggleViewAction();
    }

    The problem is that 'dock' is not defined in this function, and I can't pass it as an argument because the signal triggered() is of fixed type.

  5. #5
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: problem with Qt

    This is basic C++ and Object Oriented Programming.

    Here's what you do:
    Qt Code:
    1. FenPrincipale::FenPrincipale()
    2. {
    3. ...
    4.  
    5. // Creation of the docks
    6. QDockWidget *dock = new QDockWidget("Palette", this);
    7. addDockWidget(Qt::LeftDockWidgetArea, dock);
    8.  
    9. ...
    10. }
    To copy to clipboard, switch view to plain text mode 

    You define a QDockWidget object called dock inside the scope of your constructor. The scope is between the { and }
    This means that the object dock is only available inside this scope, thus only in the constructor.
    What you want to do is, define the object dock in a place where it is more accessible. To do that, you use the class definition. You define objects you want to use globally in your class in the definition of your class, not the implementation of your constructor.

    Example:

    Qt Code:
    1. class FenPrincipale : public QWidget
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. FenPrincipale(QWidget *parent = 0);
    7.  
    8. ...
    9.  
    10. private:
    11. QDockWidget *dock;
    12. };
    To copy to clipboard, switch view to plain text mode 

    As you see above, I've defined a private QDockWidget object called dock. (Note that the ... is just to denote that there might be more lines there).
    As it is a pointer, you have to create a new object with the new statement. This can be done in your constructor:

    Qt Code:
    1. // Look at the constructor too, I passed a parent here
    2. FenPrincipale::FenPrincipale(QWidget *parent)
    3. : QWidget(parent)
    4. {
    5. ...
    6.  
    7. // Creation of the docks
    8. // This was your line:
    9. // QDockWidget *dock = new QDockWidget("Palette", this);
    10. // This is the one I suggest:
    11. dock = new QDockWidget("Palette", this);
    12. // See the difference?
    13. addDockWidget(Qt::LeftDockWidgetArea, dock);
    14.  
    15. ...
    16. }
    To copy to clipboard, switch view to plain text mode 

    Now, you can access the object dock from every function within your class.
    Example:

    Qt Code:
    1. FenPrincipale::FenPrincipale(QWidget *parent)
    2. : QWidget(parent)
    3. {
    4. ...
    5.  
    6. // Creation of the docks
    7. // This was you line:
    8. // QDockWidget *dock = new QDockWidget("Palette", this);
    9. // This is the one I suggest:
    10. dock = new QDockWidget("Palette", this);
    11. // See the difference?
    12. addDockWidget(Qt::LeftDockWidgetArea, dock);
    13.  
    14. ...
    15. // Create a slot and connect a signal to it:
    16. connect(someActionObject, SIGNAL(triggered()), this, SLOT(showPaletteDock()));
    17. ...
    18. }
    19.  
    20. void FenPrincipale::showPaletteDock()
    21. {
    22. dock->toggleViewAction();
    23. }
    To copy to clipboard, switch view to plain text mode 

    Basic C++

  6. #6
    Join Date
    May 2010
    Posts
    30
    Qt products
    Qt4

    Default Re: problem with Qt

    seems obvious now.
    thanks a lot for your explanation, very instructive.

    I have a small unrelated problem, but I'd like to ask it here instead of making a new thread, if you don't mind, because you seem to know quite a lot about this stuff.
    I'd like to display some numbers and words in a 2 dimensional array. I tried QTableWidget but this class is far too complex, the user can edit it whereas I only want to display it. Any ideas ?

  7. #7
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: problem with Qt

    A table widget is actually the best way to display 2d data (well, I like to think so).
    You can disable the edit triggers by using:
    setEditTriggers(QAbstractItemView::NoEditTriggers)

  8. #8
    Join Date
    May 2010
    Posts
    30
    Qt products
    Qt4

    Default Re: problem with Qt

    Do you really have a fast answer to everything ?
    Is there a way I can make the cells change their sizes according to their contents?
    Similarly, Is there a way I can make the whole table change its size according to its content? (if the table is too small, scrolls appear, and I'd like to avoid that).

    I have another question that will more likely match your skills.
    I'm trying to use QTextEdit.
    First I'd like to know how to access its content and how to write stuff into QTextEdit, but I'm guessing this is easy for you.
    Now what I'd like to do is to display the line number in front of each line, as it is done in many editors. I assume this is less easy, but I hope you'll be able to help me.

    Thanks a lot for your help so far, I find it really useful and instructive.

  9. #9
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: problem with Qt

    Quote Originally Posted by deeee View Post
    but I'm guessing this is easy for you.
    It's easy for you too :-)
    Go to this site:
    http://doc.qt.nokia.com/4.6/index.html

    Quote Originally Posted by deeee View Post
    Is there a way I can make the cells change their sizes according to their contents?
    Of course:
    Use horizontalHeader () const : QHeaderView * to get the header view of your table. The header view are the column names.
    The header view has a member function to set the the resize mode, conveniently called setResizeMode(...)
    Set one or all columns to QHeaderView::ResizeToContents
    Maybe do the same for verticalHeader

    Quote Originally Posted by deeee View Post
    Similarly, Is there a way I can make the whole table change its size according to its content? (if the table is too small, scrolls appear, and I'd like to avoid that).
    I'm not sure what you mean. Do you want to make the window bigger?

    Quote Originally Posted by deeee View Post
    First I'd like to know how to access its content and how to write stuff into QTextEdit
    As said in the beginning, read the documentation. This is straightforward.

    Quote Originally Posted by deeee View Post
    Now what I'd like to do is to display the line number in front of each line, as it is done in many editors. I assume this is less easy, but I hope you'll be able to help me.
    This depends on how you want to do this. The simple, lazy way is to just prepend a line number to each text line. But when you copy and paste, you'll copy the line number too.
    If you want a separate column, you will need to subclass QTextEdit.

    Quote Originally Posted by deeee View Post
    Thanks a lot for your help so far, I find it really useful and instructive.
    Just read the documentation, most of your questions are already answered there.
    And it might be a good idea to start a new topic for different questions.

  10. #10
    Join Date
    May 2010
    Posts
    30
    Qt products
    Qt4

    Default Re: problem with Qt

    ok, thanks for these answers.
    I'm not English native so I have a few difficulties to read the documentation, sorry to bother you with my questions.
    I read (though quite fast) the whole page regarding the QTextEdit class but I didn't find how to access/modify the contents of the text.

    Regarding the size of the table, what I mean is that sometimes some blank zones appear, and sometimes there's a need to scroll to see the contents.
    Here is my code (please tell me if/how I can make it better) :
    Qt Code:
    1. int main(int argc, char *argv[])
    2. {
    3. QApplication a(argc, argv);
    4. QWidget *fenetre = new QWidget ();
    5. fenetre->setFixedSize(300,600);
    6. QTableWidget *table = new QTableWidget(3, 2, fenetre);
    7. table->setFixedSize(400,400);
    8. table->setEditTriggers(QAbstractItemView::NoEditTriggers);
    9. for (int row = 0; row < 3; row++)
    10. {
    11. for (int column = 0; column < 2; column++)
    12. {
    13. // set the special features of each item
    14. item->setText("item foogyfyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy");
    15. table->setItem(row,column,item);
    16. }
    17. }
    18. fenetre->show();
    19. return a.exec();
    20. }
    To copy to clipboard, switch view to plain text mode 
    If I do this :
    Qt Code:
    1. QWidget *fenetre = new QWidget ();
    2. fenetre->setFixedSize(300,600);
    3. QTableWidget *table = new QTableWidget(3, 2, fenetre);
    4. table->setFixedSize(200,200);
    To copy to clipboard, switch view to plain text mode 
    I get a scrollbar ;
    and if I do this :
    Qt Code:
    1. QWidget *fenetre = new QWidget ();
    2. fenetre->setFixedSize(300,600);
    3. QTableWidget *table = new QTableWidget(3, 2, fenetre);
    4. table->setFixedSize(400,400);
    To copy to clipboard, switch view to plain text mode 
    or this
    Qt Code:
    1. QWidget *fenetre = new QWidget ();
    2. fenetre->setFixedSize(300,600);
    3. QTableWidget *table = new QTableWidget(3, 2, fenetre);
    4. //table->setFixedSize(200,200);
    To copy to clipboard, switch view to plain text mode 
    I get blank zones

    I guessed I'd have to do a new class to make the line count appear as well.
    I have no idea how big this project is though.
    Can you give me a hint ? I mean, if it takes 100 code lines to implement this feature, I will drop it. Can you help me to do it please ?

    Thanks.

  11. #11
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: problem with Qt

    Quote Originally Posted by deeee View Post
    ok, thanks for these answers.
    I'm not English native so I have a few difficulties to read the documentation, sorry to bother you with my questions.
    I read (though quite fast) the whole page regarding the QTextEdit class but I didn't find how to access/modify the contents of the text.
    You're looking for these functions:
    void setHtml ( const QString & text )
    void setPlainText ( const QString & text )
    void setText ( const QString & text )

    Regarding the size of the table, what I mean is that sometimes some blank zones appear, and sometimes there's a need to scroll to see the contents.
    setFixedSize should only be used in special cases.
    You want to make use of a layout manager. Luckily Qt comes with a bunch of those.
    Example:

    Qt Code:
    1. #include <QGridLayout>
    2.  
    3. int main(int argc, char *argv[])
    4. {
    5. QApplication a(argc, argv);
    6.  
    7. QWidget *fenetre = new QWidget ();
    8.  
    9. QGridLayout *layout = new QGridLayout;
    10.  
    11. QTableWidget *table = new QTableWidget(3, 2, fenetre);
    12. table->setEditTriggers(QAbstractItemView::NoEditTriggers);
    13.  
    14. layout->addWidget(table);
    15.  
    16. fenetre->setLayout(layout);
    17.  
    18. for (int row = 0; row < 3; row++)
    19. {
    20. for (int column = 0; column < 2; column++)
    21. {
    22. // set the special features of each item
    23. item->setText("item foogyfyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy");
    24. table->setItem(row,column,item);
    25. }
    26. }
    27. fenetre->show();
    28. return a.exec();
    29. }
    To copy to clipboard, switch view to plain text mode 
    I guessed I'd have to do a new class to make the line count appear as well.
    I have no idea how big this project is though.
    Can you give me a hint ? I mean, if it takes 100 code lines to implement this feature, I will drop it. Can you help me to do it please ?
    There's another easy way, create two qtextedits next to each other and link the scrolling.
    I've seen such a widget recently, but I can't remember where.

  12. #12
    Join Date
    May 2010
    Posts
    30
    Qt products
    Qt4

    Default Re: problem with Qt

    ok great, thanks, it works.
    The layout manager seems better indeed because the blank zone is now proportional to the size of the window, which is better. But how to adapt the blank zone to the matrix and display the grey background instead ?

    I see what you mean with 2 qtextedits, but how do you link the scrolling (the text scrolls down when you press enter many times as well, wouldn't that be hard to check?) ?
    It would be great if you could either remember where you saw it, or help me to make it from scratch.

  13. #13
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: problem with Qt

    Quote Originally Posted by deeee View Post
    This button sends a triggered signal when it is clicked on, and I want to connect it to a function called display_dock that is basically like this :
    void FenPrincipale::display_dock()
    {
    dock.ToggleViewAction();
    }
    It's not how you use actions. Please read about QAction class. You just need to add toggleViewAction() to the menu or assign it to a button or wherever else you want and that's it, the action will take care of the rest.

    Sorry if that has already been said, I admit I haven't read subsequent posts in this thread yet.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


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
  •  
Qt is a trademark of The Qt Company.