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.
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.
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.
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?
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.
This is basic C++ and Object Oriented Programming.
Here's what you do:
Qt Code:
FenPrincipale::FenPrincipale() { ... // Creation of the docks addDockWidget(Qt::LeftDockWidgetArea, dock); ... }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:
{ Q_OBJECT public: ... private: QDockWidget *dock; };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:
// Look at the constructor too, I passed a parent here { ... // Creation of the docks // This was your line: // QDockWidget *dock = new QDockWidget("Palette", this); // This is the one I suggest: // See the difference? addDockWidget(Qt::LeftDockWidgetArea, dock); ... }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:
{ ... // Creation of the docks // This was you line: // QDockWidget *dock = new QDockWidget("Palette", this); // This is the one I suggest: // See the difference? addDockWidget(Qt::LeftDockWidgetArea, dock); ... // Create a slot and connect a signal to it: connect(someActionObject, SIGNAL(triggered()), this, SLOT(showPaletteDock())); ... } void FenPrincipale::showPaletteDock() { dock->toggleViewAction(); }To copy to clipboard, switch view to plain text mode
Basic C++
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 ?
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)
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.
It's easy for you too :-)
Go to this site:
http://doc.qt.nokia.com/4.6/index.html
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
I'm not sure what you mean. Do you want to make the window bigger?
As said in the beginning, read the documentation. This is straightforward.
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.
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.
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) :
If I do this :Qt Code:
int main(int argc, char *argv[]) { fenetre->setFixedSize(300,600); table->setFixedSize(400,400); for (int row = 0; row < 3; row++) { for (int column = 0; column < 2; column++) { // set the special features of each item item->setText("item foogyfyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy"); table->setItem(row,column,item); } } fenetre->show(); return a.exec(); }To copy to clipboard, switch view to plain text mode
I get a scrollbar ;Qt Code:
fenetre->setFixedSize(300,600); table->setFixedSize(200,200);To copy to clipboard, switch view to plain text mode
and if I do this :
or thisQt Code:
fenetre->setFixedSize(300,600); table->setFixedSize(400,400);To copy to clipboard, switch view to plain text mode
I get blank zonesQt Code:
fenetre->setFixedSize(300,600); //table->setFixedSize(200,200);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 ?
Thanks.
You're looking for these functions:
void setHtml ( const QString & text )
void setPlainText ( const QString & text )
void setText ( const QString & text )
setFixedSize should only be used in special cases.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.
You want to make use of a layout manager. Luckily Qt comes with a bunch of those.
Example:
Qt Code:
#include <QGridLayout> int main(int argc, char *argv[]) { layout->addWidget(table); fenetre->setLayout(layout); for (int row = 0; row < 3; row++) { for (int column = 0; column < 2; column++) { // set the special features of each item item->setText("item foogyfyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy"); table->setItem(row,column,item); } } fenetre->show(); return a.exec(); }To copy to clipboard, switch view to plain text modeThere's another easy way, create two qtextedits next to each other and link the scrolling.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 ?
I've seen such a widget recently, but I can't remember where.
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.
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.
Bookmarks