You are not logged in.

Dear visitor, welcome to KDE-Forum.org. If this is your first visit here, please read the Help. It explains in detail how this page works. To use all features of this page, you should consider registering. Please use the registration form, to register here or read more information about the registration process. If you are already registered, please login here.

boyadg

Beginner

  • "boyadg" started this thread

Posts: 11

Location: Barcelona

  • Send private message

1

Monday, May 10th 2004, 10:27pm

KStatusBar

Hi all,

I created an app following the tutorial at:
http://women.kde.org/articles/tutorials/kdevelop3/index.html

Now I want to add a statusbar to my app, but my entire user interface class is derived from QWidget instead of KMainWindow (that's how it's done in the tutorial), and I don't understand how to add a statusbar to it. All the examples I could find on the net are for KMainWindow based apps.

I already tried changing the .ui file, changing <widget class="QWidget"> to <widget class="KMainWindow">, but I got lots and lots of compile errors.

I also tried to add a KStatusBar to my app adding
m_statusbar = new KStatusBar( this );
to the constructor of the UI derived class, and that actually gave me a statusbar, but it was in the wrong place (top left of the dialog)

So my question is:

Is there a way to make my user interface derive from KMainWindow instead of QWidget without starting over?

*OR*

Is there a way to add a ststusbar to a QWidget based UI?


Thanks in advance

dfaure

Beginner

Posts: 16

Location: France

  • Send private message

2

Tuesday, May 11th 2004, 6:51am

RE: KStatusBar

The best way is to create a KMainWindow yourself in the code, and set the QtDesigner-generated widget as the central widget of the mainwindow (see QMainWindow::setCentralWidget).
David Faure, KDE developer, Klaralvdalens-Datakonsult AB.

boyadg

Beginner

  • "boyadg" started this thread

Posts: 11

Location: Barcelona

  • Send private message

3

Tuesday, May 11th 2004, 9:15am

Thanks (again) dfaure for your help! I did what you said, but some things seem to not work. Here's what I did in main():

KApplication app;

KMainWindow* mainWin = new KMainWindow();
app.setMainWidget( mainWin );

MyUIClass* centralWidget = 0;
centralWidget = new MyUIClass();
mainWin->setCentralWidget( MyUIClass );
MyUIClass->show();

return app.exec();

note: MyUIClass is derived from the class that Qt Designer creates, which in turn is derived from QWidget.

As I said, it works, but with some glitches, being the most important one that the app doesn't close well when I click on the close-button of the window. I suppose it's because I have to delete the KMainWindow, but I don't know where.

Second, the window is smaller than what I make it in Qt Designer, which means that some of the controls initially aren't visible. I suppose I have to set the size for KMainWindow. Is it possible to make it adapt automatically to the size of the central widget (what I make in Qt Designer)?

An finally, the strangest problem: My splashscreen shows up wrong. It has pieces shown twice and black lines in it. I simply use KSplashScreen.


Thanks in advance for your interest.

anda_skoa

Professional

Posts: 1,273

Location: Graz, Austria

Occupation: Software Developer

  • Send private message

4

Tuesday, May 11th 2004, 10:05am

Quoted

Originally posted by boyadg
Thanks (again) dfaure for your help! I did what you said, but some things seem to not work. Here's what I did in main():

KApplication app;

KMainWindow* mainWin = new KMainWindow();
app.setMainWidget( mainWin );

MyUIClass* centralWidget = 0;
centralWidget = new MyUIClass();
mainWin->setCentralWidget( MyUIClass );
MyUIClass->show();

return app.exec();

I think mainWin->show() would be more appropriate.

Quoted


As I said, it works, but with some glitches, being the most important one that the app doesn't close well when I click on the close-button of the window. I suppose it's because I have to delete the KMainWindow, but I don't know where.


Hmm, the application should quit if the mainWidget closes, but you can try if

Source code

1
QObject::connect(&app, SIGNAL(lastWindowClosed()), &app, SLOT(quit()));

before the call to app.exec() helps.

Quoted

the window is smaller than what I make it in Qt Designer, which means that some of the controls initially aren't visible. I suppose I have to set the size for KMainWindow. Is it possible to make it adapt automatically to the size of the central widget (what I make in Qt Designer)?

Does you widget have a proper layout?

Cheers,
_
Qt/KDE Developer
Debian User

boyadg

Beginner

  • "boyadg" started this thread

Posts: 11

Location: Barcelona

  • Send private message

5

Tuesday, May 11th 2004, 10:31am

Thanks a lot for the quick reply!!!

> "I think mainWin->show() would be more appropriate"

That was my logic too, but if I do that, a small, empty window appears. If I combine both mainWin->show() and MyUIClass->show() two windows appear, one with the UI and one empty! So I finally chose to only show MyUIClass.


> "Hmm, the application should quit if the mainWidget closes"

Maybe it's because the mainWidget has a centralWidget of its own?


> "you can try if QObject::connect(...) before the call to app.exec() helps.

Yes, that worked. Thanks!


> "Does you widget have a proper layout?"

If you mean Layouts and Spacers, well no, not until I finish the design of the UI. Is that the cause of the window being too small?


Oh and one more note about the splashscreen issue:
It worked perfectly when my code didn't contain a KMainWindow. It was after adding the KMainWindow lines that it started to show up strangely.

rhg

Beginner

Posts: 1

Location: Velbert, Germany

Occupation: senior software developer

  • Send private message

6

Tuesday, May 11th 2004, 11:59am

Quoted

Originally posted by boyadg
> "I think mainWin->show() would be more appropriate"

That was my logic too, but if I do that, a small, empty window appears. If I combine both mainWin->show() and MyUIClass->show() two windows appear, one with the UI and one empty! So I finally chose to only show MyUIClass.


This is actually a mistake in your code. You must make the MyUIClass object a child of mainWin, otherwise you get two independent toplevel windows:

Source code

1
2
MyUIClass* centralWidget = 0;
centralWidget = new MyUIClass(mainWin);
This way, you get only one window (mainWin). Now do a mainWin->show() and all is good :)

boyadg

Beginner

  • "boyadg" started this thread

Posts: 11

Location: Barcelona

  • Send private message

7

Tuesday, May 11th 2004, 9:57pm

> "This is actually a mistake in your code. You must make the MyUIClass object a child of mainWin, otherwise you get two independent toplevel windows... This way, you get only one window (mainWin). Now do a mainWin->show() and all is good

You're absolutely right, now the app closes properly. Thanks so much!

Also, I got the window to startup big enough by adding the line:
mainWin->setMinimumSize( centralWidget->size() );
I don't know if that's what I supposed to have done, but it works.

What still doesn't work well, is the splash screen. As I said, it looks garbled up, the top left corner showing the bottom right part of the image, etc. As I'm really very new to this (I suppose you noticed), I'm gonna copy the entire main function so you can see if there are any obvious errors. The fact is that the splash started to act funny when I added the KMainWindow to my code, so it's also possible that that's somehow the cause. If anyone has any ideas...

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
int main( int argc, char** argv )
{
   KAboutData about(
      "foo",
      I18N_NOOP( "Foo" ),
      version,
      description,
      KAboutData::License_GPL,
      "Coded by Newbie",
      0,
      0,
      "newbie@mail.com"
   );
   about.addAuthor( "newbie", 0, "newbie@mail.com" );

   KCmdLineArgs::init( argc, argv, &about );
   KCmdLineArgs::addCmdLineOptions( options );

   KApplication app;

   QPixmap pixmap( "splash.png" );
   KSplashScreen *splash = new KSplashScreen( pixmap );
   splash->show();

   KCmdLineArgs *args = KCmdLineArgs::parsedArgs();
   
   KMainWindow* mainWin = new KMainWindow();
   app.setMainWidget( mainWin );

   MyUIClass* centralWidget = 0;
   centralWidget = new MyUIClass( mainWin );
   
   mainWin->setCentralWidget( centralWidget );
   mainWin->setMinimumSize( centralWidget->size() );
   mainWin->show();

   args->clear();

   splash->finish( centralWidget );
   delete splash;

   return app.exec();
}


Thanks for your attention. I'm very impressed about the help I'm receiving here, but I really feel bad to bug professional developpers with stupid newbie questions. If there's any "newbie-forum" about KDE-Qt developmente, feel free to send me there.