Results 1 to 6 of 6

Thread: Window menu problem, C# & Qt

  1. #1
    Join Date
    Feb 2010
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Window menu problem, C# & Qt

    I've wasted 2 days trying to figure out why one class (UI designed in Qt Creator and then converted to C#) shows the menu and the other (written by hand, mostly copy&paste from the Qt-converted one) doesn't show the menu. Maybe a pair of fresh eyes can see what's wrong? It drives me nuts. Here are the codes:

    Made in Qt Creator:
    Qt Code:
    1. public class MainWindow : QMainWindow{
    2.  
    3. public QAction actionNew;
    4. public QAction actionOpen;
    5. public QAction actionSave;
    6. public QAction actionSaveAs;
    7. public QAction actionQuit;
    8. public QAction actionConfigGiraffe;
    9. public QAction actionConvert;
    10. public QAction actionRestart;
    11. public QAction actionAbout;
    12.  
    13. public QWidget mainWidget;
    14. public QWidget menuWidget;
    15. public QWidget toolWidget;
    16. public QWidget statusWidget;
    17. public QStackedWidget stackWidget;
    18. public QWidget startPage;
    19. public QWidget basicVideoPage;
    20. public QWidget basicAudioPage;
    21. public QWidget basicImagePage;
    22.  
    23. public QVBoxLayout mainLayout;
    24. public QSpacerItem hSpacer;
    25. public QSpacerItem vSpacer;
    26. public QSize size;
    27.  
    28. public QMenuBar menuBar;
    29. public QMenu menuFile;
    30. public QMenu menuEdit;
    31. public QMenu menuView;
    32. public QMenu menuGo;
    33. public QMenu menuTools;
    34. public QMenu menuSettings;
    35. public QMenu menuHelp;
    36. public QStatusBar statusBar;
    37. public QToolBar toolBar;
    38.  
    39. startWin.StartWindow startwin = new startWin.StartWindow();
    40. //Ui.MainWindow win = new Ui.MainWindow();
    41.  
    42. public MainWindow() {
    43. InitUI();
    44. //win.SetupUi(this);
    45. this.Show();
    46. }
    47.  
    48. public void InitUI() {
    49. this.WindowTitle = "Start - Cute Giraffe";
    50. size = new QSize(500, 400);
    51. this.Size = size;
    52. this.Move(200,200);
    53. hSpacer = new QSpacerItem(50, 20, QSizePolicy.Policy.Expanding, QSizePolicy.Policy.Expanding);
    54. vSpacer = new QSpacerItem(20, 100, QSizePolicy.Policy.Expanding, QSizePolicy.Policy.Expanding);
    55. mainWidget = new QWidget(this);
    56. //menuWidget = new QWidget(mainWidget);
    57. //toolWidget = new QWidget(mainWidget);
    58. //statusWidget = new QWidget(mainWidget);
    59. stackWidget = new QStackedWidget(mainWidget);
    60. startPage = new QWidget(stackWidget);
    61. basicVideoPage = new QWidget(stackWidget);
    62. basicAudioPage = new QWidget(stackWidget);
    63. basicImagePage = new QWidget(stackWidget);
    64.  
    65. startwin.StartUI(this, startPage);
    66.  
    67. stackWidget.AddWidget(startPage);
    68. stackWidget.AddWidget(basicVideoPage);
    69. stackWidget.AddWidget(basicAudioPage);
    70. stackWidget.AddWidget(basicImagePage);
    71. stackWidget.SetCurrentIndex(0);
    72.  
    73. mainLayout = new QVBoxLayout(mainWidget);
    74. //mainLayout.AddWidget(menuWidget);
    75. //mainLayout.AddWidget(toolWidget);
    76. mainLayout.AddItem(vSpacer);
    77. mainLayout.AddWidget(stackWidget);
    78. mainLayout.AddItem(vSpacer);
    79. //mainLayout.AddWidget(statusWidget);
    80. mainLayout.sizeConstraint = QLayout.SizeConstraint.SetDefaultConstraint;
    81.  
    82. mainWidget.SetLayout(mainLayout);
    83. this.SetCentralWidget(mainWidget);
    84.  
    85. actionNew = new QAction("&New", this);
    86. actionOpen = new QAction("&Open", this);
    87. actionSave = new QAction("&Save", this);
    88. actionSaveAs = new QAction("Save &As...", this);
    89. actionQuit = new QAction("&Quit", this);
    90. actionConfigGiraffe = new QAction("Configure &Giraffe...", this);
    91. actionConvert = new QAction("&Convert", this);
    92. actionRestart = new QAction("&Restart", this);
    93. actionAbout = new QAction("&About", this);
    94.  
    95. menuBar = new QMenuBar(this);
    96. menuBar.Geometry = new QRect(0, 0, 500, 22);
    97. menuFile = new QMenu(menuBar);
    98. menuEdit = new QMenu(menuBar);
    99. menuView = new QMenu(menuBar);
    100. menuGo = new QMenu(menuBar);
    101. menuTools = new QMenu(menuBar);
    102. menuSettings = new QMenu(menuBar);
    103. menuHelp = new QMenu(menuBar);
    104. /* menuFile.AddAction(actionNew);
    105. menuFile.AddAction(actionOpen);
    106. menuFile.AddAction(actionSave);
    107. menuFile.AddAction(actionSaveAs);
    108. menuFile.AddSeparator();
    109. menuFile.AddAction(actionQuit);
    110. */ this.SetMenuBar(menuBar);
    111.  
    112. menuBar.AddAction(menuFile.MenuAction());
    113. menuBar.AddAction(menuEdit.MenuAction());
    114. menuBar.AddAction(menuView.MenuAction());
    115. menuBar.AddAction(menuGo.MenuAction());
    116. menuBar.AddAction(menuTools.MenuAction());
    117. menuBar.AddAction(menuSettings.MenuAction());
    118. menuBar.AddAction(menuHelp.MenuAction());
    119. menuFile.AddAction(actionNew);
    120. menuFile.AddAction(actionOpen);
    121. menuFile.AddAction(actionSave);
    122. menuFile.AddAction(actionSaveAs);
    123. menuFile.AddSeparator();
    124. menuFile.AddAction(actionQuit);
    125. menuHelp.AddAction(actionAbout);
    126.  
    127. /* menuEdit = new QMenu();
    128. menuEdit = MenuBar().AddMenu("&Edit");
    129. menuView = new QMenu();
    130. menuView = MenuBar().AddMenu("&View");
    131. menuGo = new QMenu();
    132. menuGo = MenuBar().AddMenu("&Go");
    133. menuTools = new QMenu();
    134. menuTools = MenuBar().AddMenu("&Tools");
    135. menuSettings = new QMenu();
    136. menuSettings = MenuBar().AddMenu("&Settings");
    137. menuHelp = new QMenu();
    138. menuHelp = MenuBar().AddMenu("&Help");
    139. menuHelp.AddAction(actionAbout);
    140. */
    141. toolBar = new QToolBar(this);
    142. toolBar.AddAction(actionQuit);
    143. this.AddToolBar(Qt.ToolBarArea.TopToolBarArea, toolBar);
    144.  
    145. statusBar = new QStatusBar(this);
    146. this.SetStatusBar(statusBar);
    To copy to clipboard, switch view to plain text mode 

    Hand written:
    Qt Code:
    1. public class Ui_MainWindow
    2. {
    3. public QAction actionQuit;
    4. public QAction actionNew;
    5. public QAction actionOpen;
    6. public QAction actionSave;
    7. public QAction actionSave_As;
    8. public QAction actionConfigure_Giraffe;
    9. public QAction actionConvert;
    10. public QAction actionRestart;
    11. public QAction actionAbout;
    12. public QWidget centralWidget;
    13. public QHBoxLayout hboxLayout;
    14. public QMenuBar menuBar;
    15. public QMenu menuEdit;
    16. public QMenu menuView;
    17. public QMenu menuGo;
    18. public QMenu menuTools;
    19. public QMenu menuSettings;
    20. public QMenu menuHelp;
    21. public QMenu menuFile;
    22. public QStatusBar statusBar;
    23. public QToolBar toolBar;
    24.  
    25. public void SetupUi(QMainWindow MainWindow)
    26. {
    27. if (MainWindow.ObjectName == "")
    28. MainWindow.ObjectName = "MainWindow";
    29. QSize Size = new QSize(500, 400);
    30. Size = Size.ExpandedTo(MainWindow.MinimumSizeHint());
    31. MainWindow.Size = Size;
    32. actionQuit = new QAction(MainWindow);
    33. actionQuit.ObjectName = "actionQuit";
    34. actionNew = new QAction(MainWindow);
    35. actionNew.ObjectName = "actionNew";
    36. actionOpen = new QAction(MainWindow);
    37. actionOpen.ObjectName = "actionOpen";
    38. actionSave = new QAction(MainWindow);
    39. actionSave.ObjectName = "actionSave";
    40. actionSave_As = new QAction(MainWindow);
    41. actionSave_As.ObjectName = "actionSave_As";
    42. actionConfigure_Giraffe = new QAction(MainWindow);
    43. actionConfigure_Giraffe.ObjectName = "actionConfigure_Giraffe";
    44. actionConvert = new QAction(MainWindow);
    45. actionConvert.ObjectName = "actionConvert";
    46. actionRestart = new QAction(MainWindow);
    47. actionRestart.ObjectName = "actionRestart";
    48. actionAbout = new QAction(MainWindow);
    49. actionAbout.ObjectName = "actionAbout";
    50. centralWidget = new QWidget(MainWindow);
    51. centralWidget.ObjectName = "centralWidget";
    52. hboxLayout = new QHBoxLayout(centralWidget);
    53. hboxLayout.Spacing = 6;
    54. hboxLayout.Margin = 11;
    55. hboxLayout.ObjectName = "hboxLayout";
    56. MainWindow.SetCentralWidget(centralWidget);
    57. menuBar = new QMenuBar(MainWindow);
    58. menuBar.ObjectName = "menuBar";
    59. menuBar.Enabled = true;
    60. menuBar.Geometry = new QRect(0, 0, 500, 22);
    61. menuEdit = new QMenu(menuBar);
    62. menuEdit.ObjectName = "menuEdit";
    63. menuView = new QMenu(menuBar);
    64. menuView.ObjectName = "menuView";
    65. menuGo = new QMenu(menuBar);
    66. menuGo.ObjectName = "menuGo";
    67. menuTools = new QMenu(menuBar);
    68. menuTools.ObjectName = "menuTools";
    69. menuSettings = new QMenu(menuBar);
    70. menuSettings.ObjectName = "menuSettings";
    71. menuHelp = new QMenu(menuBar);
    72. menuHelp.ObjectName = "menuHelp";
    73. menuFile = new QMenu(menuBar);
    74. menuFile.ObjectName = "menuFile";
    75. MainWindow.SetMenuBar(menuBar);
    76. statusBar = new QStatusBar(MainWindow);
    77. statusBar.ObjectName = "statusBar";
    78. MainWindow.SetStatusBar(statusBar);
    79. toolBar = new QToolBar(MainWindow);
    80. toolBar.ObjectName = "toolBar";
    81. MainWindow.AddToolBar(Qt.ToolBarArea.TopToolBarArea, toolBar);
    82.  
    83. menuBar.AddAction(menuFile.MenuAction());
    84. menuBar.AddAction(menuEdit.MenuAction());
    85. menuBar.AddAction(menuView.MenuAction());
    86. menuBar.AddAction(menuGo.MenuAction());
    87. menuBar.AddAction(menuTools.MenuAction());
    88. menuBar.AddAction(menuSettings.MenuAction());
    89. menuBar.AddAction(menuHelp.MenuAction());
    90. menuHelp.AddAction(actionAbout);
    91. menuFile.AddAction(actionNew);
    92. menuFile.AddAction(actionOpen);
    93. menuFile.AddAction(actionSave);
    94. menuFile.AddAction(actionSave_As);
    95. menuFile.AddSeparator();
    96. menuFile.AddAction(actionQuit);
    97. toolBar.AddAction(actionQuit);
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Oct 2015
    Posts
    3
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Window menu problem, C# & Qt

    This is the weirdest bug i've ever found, but after lots of testing, it seems to me that a QAction whose text starts with the string "config" simply isn't shown in a QMenuBar. I don't know what causes this or what the solution could be, but I thought I'd post here in case someone else has the same problem, especially since it's such a strange one.

  3. #3
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Window menu problem, C# & Qt

    And so you brought a 6-year-old post back from the dead that has absolutely no relationship to your "problem" (except that the word QAction appears in it) just to make this incorrect comment? Why?

    I think this little bit of code demonstrates that your observation is flawed:

    Qt Code:
    1. // ActionFoo.h
    2. #pragma once
    3. #include <QtWidgets/QMainWindow>
    4.  
    5. class ActionFoo : public QMainWindow
    6. {
    7. Q_OBJECT
    8.  
    9. public:
    10. ActionFoo( QWidget *parent = 0 );
    11. ~ActionFoo( );
    12.  
    13. private slots:
    14. void configSomething();
    15. };
    16.  
    17. // ActionFoo.cpp
    18.  
    19. #include "actionfoo.h"
    20. #include <QtWidgets/QMenuBar>
    21. #include <QtWidgets/QMenu>
    22. #include <QtWidgets/QAction>
    23. #include <QtCore/QDebug>
    24.  
    25. ActionFoo::ActionFoo(QWidget *parent)
    26. : QMainWindow(parent)
    27. {
    28. QMenuBar * pMenuBar = menuBar();
    29. QMenu * pMenu = pMenuBar->addMenu( "File" );
    30.  
    31. QAction * pAction = new QAction( "config something", this );
    32. pMenu->addAction( pAction );
    33. connect( pAction, &QAction::triggered, this, &ActionFoo::configSomething );
    34.  
    35. QAction * pOtherAction = new QAction( "config other", this );
    36. pMenuBar->addAction( pOtherAction );
    37. connect( pOtherAction, &QAction::triggered, this, &ActionFoo::configSomething );
    38.  
    39. resize( 500, 500 );
    40. }
    41.  
    42.  
    43. ActionFoo::~ActionFoo()
    44. {
    45. }
    46.  
    47. void ActionFoo::configSomething()
    48. {
    49. qDebug() << "just configged something";
    50. }
    51.  
    52.  
    53. // main.cpp
    54.  
    55. #include "actionfoo.h"
    56. #include <QtWidgets/QApplication>
    57.  
    58. int main(int argc, char *argv[])
    59. {
    60. QApplication a(argc, argv);
    61. ActionFoo w;
    62. w.show();
    63. return a.exec();
    64. }
    To copy to clipboard, switch view to plain text mode 

    Capture.JPG

    Note that this works even if you change the strings to simply "config", "Config", "Configuration" or any other strings you care to use. So look somewhere else for the source of your bug.

  4. #4
    Join Date
    Oct 2015
    Posts
    3
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Window menu problem, C# & Qt

    I ended up finding what the problem was. I don't think it's the same issue as OP's, but since I came here by searching on google for my issue, I'll leave this here in case it helps the next guy.

    The issue was a manifestation of Qt's "smart" menu processing in OS X, where it detects menus with specific names and automatically assigns them to OS-specific menu items. In this case, any menu whose text starts with "config" (even something like "configfoo") automatically sets the menu entry as the application's Preferences menu -- even if you manually add it to another QMenu! I would say this overeager behavior is a bug, so I'll look for a way to report it, if it hasn't been so.

    Hope you managed to solve your own issue, OP -- sorry for hijacking your thread

    And d_stranz (I composed this reply before seeing your post), I'd ask you to be a little more careful in how you address newcomers, as such a harsh tone may discourage the engagement of those new to the community who may simply not be aware of any implicit rules they may be breaking (or just be generally misinformed, as was the case here), especially when they're commenting in good faith rather than "asking for the codez" or something. This observation is further aggravated by the fact that your additional Qt expertise did not guarantee you the knowledge required to realize that my issue was indeed legitimate, as reflected in the page I linked to in the beginning of my reply. Thanks anyway (genuinely) for taking the time to test the issue.

  5. #5
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Window menu problem, C# & Qt

    @waldyrious: My apologies. We have had a rash lately of newcomers resurrecting long-dead threads to post misinformation, and this looked like another of them. It seemed ludicrous that a menu item that happened to start with "config" would be somehow singled out for special treatment by Qt, and I felt the need to post an example to illustrate that it wasn't the case so that others would not come across it and be misled. Most of these "Qt bugs" turn out to be a problem that exists between the chair and the keyboard.

    Your first reply said nothing about the behavior being specific to OS/X, it was made to a thread that talked about C# and Qt, and so really should have been posted as a new top-level thread. In any case, I agree that it should be considered a bug if there is no way to override the behavior.

    Edit: Upon further investigation into the QMenuBar docs:

    Qt for OS X also provides a menu bar merging feature to make QMenuBar conform more closely to accepted OS X menu bar layout. The merging functionality is based on string matching the title of a QMenu entry. These strings are translated (using QObject::tr()) in the "QMenuBar" context. If an entry is moved its slots will still fire as if it was in the original place. The table below outlines the strings looked for and where the entry is placed if matched:

    You can override this behavior by using the QAction::menuRole() property.
    So basically, it isn't a bug, it's a "feature".
    Last edited by d_stranz; 29th April 2016 at 16:00.

  6. #6
    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: Window menu problem, C# & Qt

    @waldyrious. It might help if you edit your forum profile to indicate that you use OS X rather than just X and Windows. This helps when you don't specify the platform in your post (that would help too)

    I was surprised by the original post: it appears someone is converting generated C++ to C# in order to use it on X11/UNIX presumably in Mono... at least if their profile is to be believed.
    Last edited by ChrisW67; 29th April 2016 at 22:12.

Similar Threads

  1. Disable window menu on a QDialog
    By febil in forum Qt Programming
    Replies: 5
    Last Post: 24th June 2011, 00:58
  2. menu problem
    By addu in forum Qt Programming
    Replies: 9
    Last Post: 9th May 2009, 17:00
  3. QWT right click window.. (Context Menu)
    By maveric in forum Qt Programming
    Replies: 4
    Last Post: 25th May 2008, 08:07
  4. copy/paste actions in main window menu
    By magland in forum Qt Programming
    Replies: 3
    Last Post: 2nd October 2007, 12:33
  5. How to change system menu of window?
    By krivenok in forum Qt Programming
    Replies: 1
    Last Post: 27th January 2006, 15:19

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.