Results 1 to 7 of 7

Thread: Layouts and Window sizes problem

  1. #1
    Join Date
    Mar 2013
    Location
    Hyderabad,Bangalore,India
    Posts
    70
    Thanks
    8
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Layouts and Window sizes problem

    I am creating an application which has some widgets like display window , keypad and log window.

    display window to display some external application
    keypad to control application
    log window to display logs

    the difficulty i am facing is with layouts and window sizes
    i want to see my interface look same way in minimized and maximized state , i mean
    all borders of windows.

    Images are attached.

    Problem 1:
    when in minimized state
    At the right most side(between DispWin and KeyWin) and at the bottom just above the LogWin(between LogWin and DispWin), i cannot see border of MainWin(i.e Cyan color)

    Problem 2:
    when in maxiimized state and in minimized state
    The keypad height is exceding the height of MainWin despite setting proper height.

    I want to see all the borders of windows( ie Cyan color around DispWin, LogWin and NavyBlue around MainWin and KeyWin.

    I tried in various ways , but could not achieve.

    please help...

    Qt Code:
    1. #define KEYPAD_WIDTH 200
    2. #define KEYPAD_HEIGHT 700
    3.  
    4. #define LOGWINDOW_HEIGHT 80
    5.  
    6. #define MIN_WIDTH_FACTOR 240
    7. #define MIN_HEIGHT_FACTOR 100
    8.  
    9. MainWindow::MainWindow(QWidget *parent)
    10. : QMainWindow(parent)
    11. {
    12. int x_c, y_c, screen_width,screen_height;
    13. int display_window_width,display_window_height;
    14.  
    15.  
    16. ServerWin = new QWidget;
    17. MainWin = new QWidget;
    18. DispWin = new QWidget;
    19. LogWin = new QWidget;
    20. KeyWin = new QWidget;
    21.  
    22. QHBoxLayout *Mlayout = new QHBoxLayout;
    23. QVBoxLayout *Dlayout = new QVBoxLayout;
    24.  
    25. QRect rect = D->geometry();
    26. rect.getRect(&x_c,&y_c,&screen_width,&screen_height);
    27. qDebug()<<"x="<<x_c<<"y="<<y_c<<"s_w="<<screen_width<<"s_h="<<screen_height<<endl;
    28. display_window_width = screen_width - MIN_WIDTH_FACTOR - KEYPAD_WIDTH;
    29. display_window_height = screen_height- MIN_HEIGHT_FACTOR - LOGWINDOW_HEIGHT;
    30.  
    31.  
    32. setMinimumSize(screen_width - MIN_WIDTH_FACTOR,screen_height-MIN_HEIGHT_FACTOR);
    33. setMaximumSize(screen_width, screen_height);
    34. //setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);
    35.  
    36. ServerWin->setMinimumSize(screen_width - MIN_WIDTH_FACTOR,screen_height-MIN_HEIGHT_FACTOR);
    37. ServerWin->setMaximumSize(screen_width, screen_height);
    38. setCentralWidget(ServerWin);
    39. ServerWin->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);
    40. setPalette(QPalette(QColor(0,0,128,255)));//Navy Blue
    41. setAutoFillBackground(true);
    42.  
    43.  
    44. MainWin->setPalette(QPalette(QColor(0,255,255,255)));//Cyan
    45. MainWin->setAutoFillBackground(true);
    46.  
    47. Dlayout->addWidget(DispWin);
    48. DispWin->setPalette(QPalette(QColor( 230,230,250,255)));//Lavender
    49. DispWin->setAutoFillBackground(true);
    50. Dlayout->setSpacing(1);
    51. DispWin->setMinimumSize(display_window_width,display_window_height);
    52.  
    53. Dlayout->addWidget(LogWin);
    54. LogWin->setPalette(QPalette(QColor(148,0,211,255)));//Dark Violet
    55. LogWin->setAutoFillBackground(true);
    56. LogWin->setMinimumSize(display_window_width,LOGWINDOW_HEIGHT);
    57.  
    58. MainWin->setLayout(Dlayout);
    59.  
    60. KeyWin->setPalette(QPalette(QColor( 238,130,238,255)));//Violet
    61. KeyWin->setAutoFillBackground(true);
    62.  
    63. //QWidget* keypad = new QWidget;
    64. //QHBoxLayout *vLay = new QHBoxLayout;
    65. //keypad->setLayout(vLay);
    66.  
    67. //KeyWin->setFixedSize(KEYPAD_WIDTH,KEYPAD_HEIGHT+20);
    68. //KeyWin->setFixedSize(KEYPAD_WIDTH,(display_window_height+LOGWINDOW_HEIGHT/*+MIN_HEIGHT_FACTOR*/));
    69. //KeyWin->setFixedSize(KEYPAD_WIDTH,display_window_height+LOGWINDOW_HEIGHT /*(screen_height-MIN_HEIGHT_FACTOR+display_window_height-100)*/);
    70. KeyWin->setFixedSize(KEYPAD_WIDTH,DispWin->height()+LogWin->height() /*(screen_height-MIN_HEIGHT_FACTOR+display_window_height-100)*/);
    71. //KeyWin->setMinimumSize(KEYPAD_WIDTH,(screen_height-MIN_HEIGHT_FACTOR));
    72. //vLay->addWidget(KeyWin);
    73. vLay->setContentsMargins(0,0,0,0);
    74.  
    75. Mlayout->addWidget(MainWin);
    76. Mlayout->setSpacing(1);
    77. Mlayout->addWidget(KeyWin);
    78. ServerWin->setLayout(Mlayout);
    79. }
    To copy to clipboard, switch view to plain text mode 
    Attached Images Attached Images

  2. #2
    Join Date
    Mar 2013
    Location
    Hyderabad,Bangalore,India
    Posts
    70
    Thanks
    8
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Layouts and Window sizes problem

    I know , it may be difficult or rather boring to see this kind of post, but i have tried in various ways, yet could not get what i wanted.

    I will explain in simple terms , so that i might get some reply.

    what i want is to display all windows with the borders no matter they are in MinimumSize or MaximumSize.

    the problem here is when it is in MinimumSize the windows DispWin, and LogWin are not completely seen, the KeyWin overlaps these two windows.
    when Maximized every thing is seen with border.

    I want the same look that shows in MinimumSize state even if the window is in MinimumSize, ofcourse the windows are small , but should be able to see full windows.

  3. #3
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Layouts and Window sizes problem

    Your code does nothing to make it clear what you are trying to do without someone spending a fair amount of time just decoding it, and that's after they get it to compile. Your simple terms don't really help. There are no "borders" in your code either as QFrame objects or QSS styles.

    The "borders" you are complaining about appear to be the inter-widget gaps that show the underlying widget's colour. If child widgets are overlapping then that is a certain sign that something is wrong with the layouts; but I don't see overlapping widgets. I do see that you have set the inter widget spacing in both layouts to one pixel, which would easily explain not seeing the underlying widget through the gap. I see that you are trying to force the size of certain widgets, leaving the layout engine with no option but to do something awkward.

    BTW: Your code uses vLay uninitialized. Your code leaks QDesktopWidget memory.

    Here is my take on what you are trying to achieve.
    Qt Code:
    1. MainWindow::MainWindow(QWidget *parent)
    2. : QMainWindow(parent)
    3. {
    4. ServerWin = new QWidget(this);
    5. ServerWin->setObjectName("ServerWin");
    6. QRect geom = QApplication::desktop()->availableGeometry();
    7. geom.adjust(0, 0, -MIN_WIDTH_FACTOR, -MIN_HEIGHT_FACTOR);
    8. ServerWin->setMinimumSize(geom.size());
    9.  
    10. MainWin = new QWidget(this);
    11. MainWin->setObjectName("MainWin");
    12.  
    13. DispWin = new QWidget(this);
    14. DispWin->setObjectName("DispWin");
    15.  
    16. LogWin = new QWidget(this);
    17. LogWin->setObjectName("LogWin");
    18. LogWin->setMinimumHeight(LOGWINDOW_HEIGHT);
    19. LogWin->setMaximumHeight(LOGWINDOW_HEIGHT);
    20. LogWin->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
    21.  
    22. KeyWin = new QWidget(this);
    23. KeyWin->setObjectName("KeyWin");
    24. KeyWin->setMinimumWidth(KEYPAD_WIDTH);
    25. KeyWin->setMaximumWidth(KEYPAD_WIDTH);
    26. KeyWin->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Preferred);
    27.  
    28.  
    29. QVBoxLayout *vLayout = new QVBoxLayout(this);
    30. vLayout->addWidget(DispWin);
    31. vLayout->addWidget(LogWin);
    32. MainWin->setLayout(vLayout);
    33.  
    34. QHBoxLayout *hLayout = new QHBoxLayout(this);
    35. hLayout->addWidget(MainWin);
    36. hLayout->addWidget(KeyWin);
    37. ServerWin->setLayout(hLayout);
    38.  
    39. setCentralWidget(ServerWin);
    40.  
    41. setStyleSheet(
    42. "#ServerWin { background: navy; }"
    43. "#MainWin { background: cyan; }"
    44. "#DispWin { background: lavender; }"
    45. "#LogWin { background: darkviolet; }"
    46. "#KeyWin { background: violet; }"
    47. );
    48. }
    To copy to clipboard, switch view to plain text mode 
    There are better ways to get a border on a lot of widgets (not bare QWidgets though).
    Last edited by ChrisW67; 29th October 2013 at 08:38. Reason: Changed where QDesktopWidget obtained from

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

    PstdEr (29th October 2013)

  5. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Layouts and Window sizes problem

    Btw, the desktop widget is usually acquired through QApplication::desktop()

    Cheers,
    _

  6. #5
    Join Date
    Mar 2013
    Location
    Hyderabad,Bangalore,India
    Posts
    70
    Thanks
    8
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Layouts and Window sizes problem

    Quote Originally Posted by ChrisW67 View Post
    Your code does nothing to make it clear what you are trying to do without someone spending a fair amount of time just decoding it, and that's after they get it to compile. Your simple terms don't really help. There are no "borders" in your code either as QFrame objects or QSS styles.
    The "borders" you are complaining about appear to be the inter-widget gaps that show the underlying widget's colour.
    Really sorry about that, i am not a frequent coder in Qt and dont know the basics.

    Quote Originally Posted by ChrisW67 View Post
    BTW: Your code uses vLay uninitialized. Your code leaks QDesktopWidget memory.
    Thanks for pointing, i could have used normal object instead of pointer

    And thanks a lot, Its really beautiful after your changes are applied.


    Added after 9 minutes:


    I have a doubt here, is it the correct way of doing , because it gives a warning when running
    " QLayout: Attempting to add QLayout "" to MainWindow "", which already has a layout "
    this ( mainwindow) is already having a layout and we are again trying to set one more layout.

    Quote Originally Posted by ChrisW67 View Post
    QVBoxLayout *vLayout = new QVBoxLayout(this);
    QHBoxLayout *hLayout = new QHBoxLayout(this);
    Last edited by PstdEr; 29th October 2013 at 09:13. Reason: Content Repeated

  7. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Layouts and Window sizes problem

    Just create the layouts without parent.

    Providing a parent is equivalent to calling parent->setLayout(), but QMainWindow already has a layout.

    Cheers,
    _

  8. #7
    Join Date
    Mar 2013
    Location
    Hyderabad,Bangalore,India
    Posts
    70
    Thanks
    8
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Layouts and Window sizes problem

    Sorry one more question, I dont have any where else to go .

    I am using the following code to display external applications onto DispWin of previous application by passing its width and height.

    The problem is after displaying the external app , if i maximize the window of main application , the external app does not resize to DispWin size, it stays same as when its displayed.

    is there any thing i am doing wrong from below.

    If you have time , please have a look ...

    Qt Code:
    1. int create_window(int width, int height)
    2. {
    3. XSetWindowAttributes xswa;
    4. static int resx, resy;
    5. mDisplay = XOpenDisplay(NULL);
    6. if(mDisplay == NULL){
    7. printf("m_Display Failed");
    8. return 0;
    9. }
    10. mScreen = DefaultScreen(mDisplay);
    11.  
    12. xswa.background_pixel=BlackPixel(mDisplay, mScreen);
    13. xswa.border_pixel=WhitePixel(mDisplay, mScreen);
    14. xswa.event_mask=ExposureMask|KeyPressMask|KeyReleaseMask|ButtonPressMask|ButtonReleaseMask|StructureNotifyMask;
    15. xswa.override_redirect = False;
    16.  
    17. mRootWin = DefaultRootWindow(mDisplay);
    18. if (!(window = XCreateWindow(mDisplay, mRootWin, 0, 0, width, height, 0, 0, InputOutput, DefaultVisual(mDisplay, mScreen),
    19. CWEventMask|CWBackPixel|CWBorderPixel|CWOverrideRedirect, &xswa)))
    20. {
    21. fprintf(stderr, "[x11] Failed to create window\n");
    22. return 0;
    23. }
    24. XMapWindow(mDisplay, window);
    25. while (1)
    26. {
    27. XEvent event;
    28. XNextEvent(mDisplay, &event);
    29. if (event.type==Expose)
    30. break;
    31. }
    32.  
    33. {
    34. XSizeHints SizeHints;
    35. SizeHints.flags=PMinSize|PMaxSize|USSize;
    36. SizeHints.min_width = width;// does not have any effect
    37. SizeHints.min_height = height ;// does not have any effect
    38. SizeHints.max_width= width;
    39. SizeHints.max_height= height;
    40. XSetWMNormalHints(mDisplay, window, &SizeHints);
    41. }
    42.  
    43. return 1;
    44. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #define DEFAULT_SCENE_WIDTH 1280
    2. #define DEFAULT_SCENE_HEIGHT 720
    3.  
    4. int main(int argc, char **argv)
    5. {
    6.  
    7. int width;
    8. int height;
    9. int retVal =0;
    10. qDebug()<<"argc"<<argc;
    11. for(int i= 0;i<argc;i++)
    12. qDebug()<<"argv["<<i<<"]= "<<argv[i];
    13.  
    14. QRectF sceneRect(0,0,DEFAULT_SCENE_WIDTH,DEFAULT_SCENE_HEIGHT);
    15.  
    16. QApplication app(argc, argv);
    17.  
    18. scene.setSceneRect(sceneRect);
    19.  
    20. QGraphicsView *gView = new QGraphicsView(&scene);
    21.  
    22. QWebView *wView = new QWebView();
    23. wView->load(QUrl("http://127.0.0.1/setting.html"));
    24. wView->grabKeyboard();
    25.  
    26. WId wid;
    27. if(argc < 2)
    28. {
    29. printf("pass window arg\n");
    30. return 0;
    31. }
    32. wid = app.arguments()[1].toInt();
    33. embed = new QWidget();
    34. QVBoxLayout* vl = new QVBoxLayout();
    35. embed->setLayout(vl);
    36. vl->addWidget(wView);
    37. embed->show();
    38.  
    39. width = app.arguments()[2].toInt();
    40. height = app.arguments()[3].toInt();
    41. embed->setMinimumSize(width,height);
    42. embed->setSizePolicy(QSizePolicy::Preferred,QSizePolicy::Preferred);
    43.  
    44. qDebug()<<"width & height received:"<<width<<"&"<<height;
    45.  
    46. retVal = create_window(width,height);
    47. if (0==retVal){
    48. qDebug()<<"create_window failed";
    49. return 0;
    50. }
    51.  
    52. XReparentWindow(mDisplay,embed->winId(),window,0,0);
    53. XReparentWindow(mDisplay,window,wid,0,0);
    54. XFlush(mDisplay);
    55. app.exec();
    56. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Why a restored window doesn't keep it's state (sizes)?
    By evgeniy in forum Qt Programming
    Replies: 0
    Last Post: 3rd December 2011, 07:47
  2. QWorkspace: child window layouts?
    By LiCodeX in forum Qt Programming
    Replies: 1
    Last Post: 13th November 2008, 07:50
  3. window sizes changing even though max size defined
    By JonathanForQT4 in forum Newbie
    Replies: 3
    Last Post: 6th August 2007, 10:39
  4. Changing window's sizes
    By Salazaar in forum Newbie
    Replies: 6
    Last Post: 27th July 2007, 16:23
  5. Layouts and allowing view to stretch with main window
    By steg90 in forum Qt Programming
    Replies: 1
    Last Post: 15th May 2007, 10:30

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.